总结一些因素在R中的总计数

Sum up some factors total counts in R

R 的新手!

我有一个调查,有人回答从 0 到 10。我想加起来有多少人 <= 6。有多少 7 和 8。有多少 >=9。

我不得不将问题(Return、可信赖...)转化为一个因素,以制作 x 轴上 1 到 10 的 ggplots。

uk_super_q<-read.csv("SUPR_Q_UK.csv", header = TRUE)

uk_super_q.Return <- as.factor(uk_super_q$Return)
uk_super_q.Trustworthy <- as.factor(uk_super_q$Trustworthy)
uk_super_q.Credible <- as.factor(uk_super_q$Credible)
uk_super_q.Trustworthy <- as.factor(uk_super_q$Trustworthy)
uk_super_q.Clean.and.Simple <- as.factor(uk_super_q$Clean.and.Simple)
uk_super_q.Easy.to.use <- as.factor(uk_super_q$Easy.to.use)
uk_super_q.Attractive <- as.factor(uk_super_q$Attractive)
uk_super_q.NPS <- as.factor(uk_super_q$NPS)

uk_super_q$Return <- as.factor(uk_super_q$Return)
ggplot(uk_super_q, aes(x = Return)) +
  geom_bar() +
  xlab("Return") +
  ylab("Total Count") +
  labs(fill = "Blah") 
table(uk_super_q.Return)

uk_super_q$Easy.Nav <- as.factor(uk_super_q$Easy.Nav)
ggplot(uk_super_q, aes(x = Easy.Nav)) +
  geom_bar() +
  xlab("Easy.Nav") +
  ylab("Total Count") +
  labs(fill = "Blah") 
table(uk_super_q.Trustworthy)

uk_super_q$Credible <- as.factor(uk_super_q$Credible)
ggplot(uk_super_q, aes(x = Credible)) +
  geom_bar() +
  xlab("Credible") +
  ylab("Total Count") +
  labs(fill = "Blah") 
table(uk_super_q.Credible)

uk_super_q$Attractive <- as.factor(uk_super_q$Attractive)
ggplot(uk_super_q, aes(x = Attractive)) +
  geom_bar() +
  xlab("Attractive") +
  ylab("Total Count") +
  labs(fill = "Blah") 
table(uk_super_q.Attractive)

uk_super_q$Trustworthy <- as.factor(uk_super_q$Trustworthy)
ggplot(uk_super_q, aes(x = Trustworthy)) +
  geom_bar() +
  xlab("Trustworthy") +
  ylab("Total Count") +
  labs(fill = "Blah") 
table(uk_super_q.Trustworthy)

uk_super_q$Clean.and.Simple <- as.factor(uk_super_q$Clean.and.Simple)
ggplot(uk_super_q, aes(x = Clean.and.Simple)) +
  geom_bar() +
  xlab("Clean.and.Simple") +
  ylab("Total Count") +
  labs(fill = "Blah") 
table(uk_super_q.Clean.and.Simple)

uk_super_q$Easy.to.use <- as.factor(uk_super_q$Easy.to.use)
ggplot(uk_super_q, aes(x = Easy.to.use)) +
  geom_bar() +
  xlab("Easy.to.use") +
  ylab("Total Count") +
  labs(fill = "Blah") 
table(uk_super_q.Easy.to.use)

uk_super_q$NPS <- as.factor(uk_super_q$NPS)
ggplot(uk_super_q, aes(x = NPS)) +
  geom_bar() +
  xlab("NPS") +
  ylab("Total Count") 

table(uk_super_q.NPS)

将逻辑语句应用于 data.frame returns 一个 TRUE/FALSE 值的矩阵,这些值在 R 中分别编码为 1 和 0。这使您可以使用 sum 或更有效地使用 colSums.

来计算每列中 TRUE 值的数量
colSums(uk_super_q <= 6)
colSums(uk_super_q >= 7 & uk_super_q <= 8)
colSums(uk_super_q >= 9)