【问题标题】:Using map_df from purrr on a factor column在因子列上使用来自 purrr 的 map_df
【发布时间】: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

标签: r tidyverse


【解决方案1】:

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

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
  
}

【讨论】:

    猜你喜欢
    • 2023-02-19
    • 2018-07-03
    • 2020-02-03
    • 1970-01-01
    • 2018-10-11
    • 1970-01-01
    • 2021-10-09
    • 2021-03-21
    • 1970-01-01
    相关资源
    最近更新 更多