r - 在因子列上使用来自 purrr 的 map_df
问题描述
我正在尝试计算都属于列中四个因素之一的行的多列中的响应数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
,这是对最后一列因素的引用。有没有办法将所有行保持在同一个小标题中,或者它们是否必须针对每个因素分别位于不同的小标题中?
恐怕没有其他建议的问题与我的查询完全匹配。
这是我以 rds 格式使用的数据框!
https://www.dropbox.com/s/nwq913lw13kxyw9/inallwaves.rds?dl=0
解决方案
我发现只是将列添加回来效果最好!
tally_reader_number <- function(input_dataframe,newspaper_name) {
#function takes the input of in_all_waves, tallies the number of different eu ref responses using map_df for a given newspaper factor (defined above)
# and returns a dataframe of responese for each wave with the newspaper factor as a column
returned_dataframe <- input_dataframe %>%
filter(Paper == newspaper_name) %>%
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
mutate(Paper = newspaper_name)
returned_dataframe$Paper <- as.factor(returned_dataframe$Paper)
returned_dataframe$resp <- as.factor(returned_dataframe$resp)
returned_dataframe
}
推荐阅读
- python - 在python中的多个列上循环和融化?
- asp.net-core - 试图强制 ASP.NET 核心 MVC 使用 HTTPS
- mongodb - 将 ISO 格式日期时间转换为 mongoDB 3.6 中的日期
- tensorflow - tensorflow 与 anaconda 和 python 版本冲突
- user-interface - 光子碰撞问题
- d3.js - 使用 scala.js 创建条形图
- python-3.x - tkinter 网格识别不起作用,无法识别每个像素
- android - gradle build 未检测到 build.gradle 中的 android ndkVersion
- sql - 通过 pl/sql 过程将带有行间距和空值的逗号分隔值拆分为表中的列
- html - css not showing completely in gmail