【发布时间】:2020-07-01 00:48:51
【问题描述】:
我正在尝试计算多列中的响应数,这些行都属于Paper 列中的四个因素之一。我可以使用 purr 中的 map_df 单独总结每个因素的术语
times <- in_all_waves %>%
filter(Paper =='Times') %>%
ungroup() %>% #function refuses to work without this
select(-Paper) %>%
map_df(table) %>% # use map_df from the purrr package to "table" each column
rownames_to_column("response") %>% #convert the rownames to a column named response
mutate(resp = case_when(response == 1 ~ "Remain", #change the resulting numbers to the correct responses
response == 2 ~ "Leave",
response ==3 ~ "Will Not Vote",
response == 4 ~ "Don't Know")) %>%
select(resp, everything(), -response) #reorder the columns with resp at the front, removing response
但是当我尝试这样做而不选择只选择一列时:
different_papers <- in_all_waves %>%
map_df(table) %>%
rownames_to_column("response") %>%
mutate(resp = case_when(response == 1 ~ "Remain", #change the resulting 1s to No in resp
response == 2 ~ "Leave",
response ==3 ~ "Will Not Vote",
response == 4 ~ "Don't Know")) %>%
select(resp, everything(), -response) #reorder the columns with resp at the front, removing response
我收到错误Error: Argument 9 must be length 4, not 5,这是对最后一列因素的引用。有没有办法将所有行保持在同一个 tibble 中,或者它们是否必须针对每个因素分别位于不同的行中?
恐怕没有其他建议的问题与我的查询完全匹配。
这是我使用的 rds 格式的数据框!
https://www.dropbox.com/s/nwq913lw13kxyw9/inallwaves.rds?dl=0
【问题讨论】:
-
这两个代码都没有给我任何错误。
-
我可以看看你完成的
different_papers数据框是什么样的吗?第一个 sn-p 代码对我有用,但对第二个无效.. -
你可以在这里查看 - ibb.co/gz0YWpF