【问题标题】:How to use the names_to argument in pivot_longer()如何在 pivot_longer() 中使用 names_to 参数
【发布时间】:2020-11-08 15:12:03
【问题描述】:

我在关于 names_to 参数的 SO 上看到了其他几个类似的问题;但是,我仍然不确定我是否了解如何使用它。

考虑下面的df:

df <- data.frame("Metric" = c("a_exp_2001","a_inc_2001","a_inc_2002","a_exp_2002"),
                "John" = c(220,230,240,250),
                "Abby" = c(440,450,470,480))

head(df)

      Metric John Abby
1 a_exp_2001  220  440
2 a_inc_2001  230  450
3 a_inc_2002  240  470
4 a_exp_2002  250  480

我希望创建一个新的数据框,该数据框仅包含一年(比如 2002 年)的上述信息,但格式较长。也就是说,我希望得到 3 列“姓名”、“支出”(其中值对应于a_exp_2002)、“收入”(其中值对应于a_inc_2002)。

我不知道如何在 pivot_longer() 中指定 names_to 参数来获得最终的 df。

我只能到此为止:

df %>% 
   pivot_longer(cols = -c(Metric), ...)

【问题讨论】:

    标签: r tidyverse


    【解决方案1】:

    这样可以吗?

    library(tidyverse)
    
    data <- tibble::tribble(
             ~Metric.John.Abby,
      "1 a_exp_2001  220  440",
      "2 a_inc_2001  230  450",
      "3 a_inc_2002  240  470",
      "4 a_exp_2002  250  480"
      ) %>% 
      tidyr::separate("Metric.John.Abby", into = c('num', 'metric', 'sp', 'john', 'sp2', 'abby'), sep = " ") %>% 
      select(metric, john, abby) 
    
    data %>% 
      filter(str_detect(metric, "2002")) %>% 
      pivot_longer(cols = -metric, names_to = "names") %>% 
      separate(metric, into = c('letter', 'type', 'year')) %>% 
      select(type, names, value) %>% 
      pivot_wider(type, id_cols = c('type', 'names')) %>% 
      relocate(exp, .before = inc)
    #> # A tibble: 2 x 3
    #>   names exp   inc  
    #>   <chr> <chr> <chr>
    #> 1 john  250   240  
    #> 2 abby  480   470
    

    reprex package (v0.3.0) 于 2020 年 11 月 8 日创建

    【讨论】:

      【解决方案2】:

      如果我正确理解了您的问题,这将是一个可能的解决方案:

      library(dplyr)
      library(tidyr)
      
      df <- data.frame("Metric" = c("a_exp_2001","a_inc_2001","a_inc_2002","a_exp_2002"),
                       "John" = c(220,230,240,250),
                       "Abby" = c(440,450,470,480))
      
      df %>% 
        # get relevant info from Metric in two columns
        dplyr::mutate(IN_or_EX =  substr(Metric, start = 3, stop = 5),
                      YEAR = substr(Metric, start = 7, stop = 10)) %>% 
        # filter for the year you wan
        dplyr::filter(YEAR == 2001) %>% 
        # drop the Metric column as it presents no value anymore
        dplyr::select(-Metric) %>%
        # make it longer
        tidyr::pivot_longer(-c(IN_or_EX, YEAR), names_to = "NAME", values_to = "VALUES") %>% 
        # make it wider
        tidyr::pivot_wider(names_from = IN_or_EX, values_from = VALUES, values_fill = 0)
      
      
        YEAR  NAME    exp   inc
        <chr> <chr> <dbl> <dbl>
      1 2001  Abby    440   450
      2 2001  John    220   230
      

      【讨论】:

        【解决方案3】:

        您是否正在寻找:

        pivot_longer(df, -1) %>% 
          separate(Metric, sep = 6, into = c("type", "year")) %>% 
          pivot_wider(names_from = type, values_from = value) %>%
          rename(Expenditure = a_exp_, Income = a_inc_)
        #> # A tibble: 4 x 4
        #>   year  name  Expenditure Income
        #>   <chr> <chr>       <dbl>  <dbl>
        #> 1 2001  John          220    230
        #> 2 2001  Abby          440    450
        #> 3 2002  John          250    240
        #> 4 2002  Abby          480    470
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-08-15
          • 2021-05-13
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多