-1
library(dplyr)
library(plyr)
df = data.frame(x = sample(c("Large","Medium", "Small"), 10, replace = TRUE), y = sample(c("Yes","No"), 10, replace = TRUE), z = sample(c("High","Low"), 10, replace = TRUE))
df %>%
count('x') %>%
ggplot(aes(x,freq)) +
geom_bar(stat = "identity") -> a
df %>%
count('y') %>%
ggplot(aes(y,freq)) +
geom_bar(stat = "identity") -> b
df %>%
count('z') %>%
ggplot(aes(z,freq)) +
geom_bar(stat = "identity") -> c
grid.arrange(a,b,c, ncol=3, nrow =1)
而不是写上面的代码两次,我想建立一个循环像下面给出:要构建for循环,如何在count()命令中使用变量名称?
for (val in names(df)) {
df %>%
count(get(val)) %>%
ggplot(aes(get(val),freq)) +
geom_bar(stat = "identity")
}
我收到一个错误:“错误在mutate_impl(。数据,点):对象“× ' 未找到”。
'%>%'是'包dplyr' '计数()'函数是'plyr' 我也以我的问题包括在此。 –