首页 > 解决方案 > 在因子列上使用来自 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

标签: rtidyverse

解决方案


我发现只是将列添加回来效果最好!

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
  
}


推荐阅读