我正在尝试编写一个小功能,以从一组调查表项目中列出比例表,这些表是有序因素。我对每个项目执行table()
,将其转换为数据帧,然后将其变宽。然后,我打算将它们放在一张桌子中。
此示例的输出如下所示:
item not enough just right too much
1 nl_netops_groupcom_1 0.2692308 0.6923077 0.03846154
2 nl_netops_groupcom_2 0.2000000 0.8000000 0.00000000
3 nl_netops_groupcom_3 0.4166667 0.5833333 0.00000000
但是,当我尝试bind_rows()
数据帧时,似乎找不到数据帧对象。这是该函数的代码:
make.freq.tab <- function(df.name=df_tp1, ...) {
the.vars <- df_tp1 %>% select(...) %>% colnames()
for(the.var in the.vars) {
the.tab <- as.data.frame(table(df.name[, the.var])) %>%
mutate(pcp=Freq / sum(Freq)) %>%
select(-Freq) %>%
spread(key=Var1, value=pcp) %>%
mutate(item=the.var) %>%
as.data.frame(.)
assign(the.var, the.tab[, c(4, 1, 2, 3)])
}
return(bind_rows(syms(the.vars)))
}
但是当我运行它时,就像这样:
make.freq.tab(df_tp1, nl_netops_groupcom_1:nl_netops_groupcom_3)
我收到此错误:
Error in bind_rows_(x, .id) : object 'nl_netops_groupcom_1' not found
因为使用the.var
既是字符变量又是数据框名称是我使语言处理器感到困惑吗?我真的很想这样做,因为使用the.vars
作为列名的字符串以及绑定在一起的数据帧的名称非常方便。
帮助将不胜感激。
答案 0 :(得分:0)
除了@aosmith的mget
建议之外,这里还有一个purrr
解决方案(由于您使用的是tidyverse习语-此外,请注意,您想修改对{{1} }中的{} {},因为这是自变量):
df_tp1
@aosmith的建议:
df.frame