【问题标题】:How can I unnest a vector from a dataframe?如何从数据框中取消嵌套向量?
【发布时间】:2021-09-23 06:04:57
【问题描述】:

我有一个长格式的数据框,其中包含公司及其按地区划分的估计值。我想建立一个宽表,我可以在其中查看按地区对公司进行了多少正面和负面的估计。当我尝试pivot_wider 时,我收到一个数据框,其单元格中有向量。没关系,但是我无法按地区计算正面和负面反馈的数量。还尝试使用 unnestunnest_longer 函数。后者虽然似乎解决了我的问题,但它只需要一个参数来取消列。

我怎样才能修改我的pivot_wider 以获得理想的结果?

我的数据框:

set.seed(1407)
test_df <- data.frame(code = rep(c("positive", "negative"), 9),
                      company = c("Google", "Amazon", "SpaceX", "BlueOrigin",
                                  "Google", "Western Digital", "Aliexpress",
                                  "Tencent", "Aliexpress"),
                      n = rbinom(18, size = 9, prob = 0.5),
                      region = c("Asia", "Europe", "Middle East")) 

我使用函数来加宽表格的结果:

test_df %>% 
  pivot_wider(id_cols = region,
              names_from = code,
              values_from = n)

# A tibble: 3 x 3
  region      positive  negative 
  <chr>       <list>    <list>   
1 Asia        <int [3]> <int [3]>
2 Europe      <int [3]> <int [3]>
3 Middle East <int [3]> <int [3]>

我想要的输出:

region      positive  negative

Asia            4        2
Asia            3        5
Asia            5        2
Europe          3        5
Europe          6        4
Europe          5        1
Middle East     8        5
Middle East     6        5
Middle East     6        2

【问题讨论】:

    标签: r data-manipulation


    【解决方案1】:

    您可以通过以下方式修改您的解决方案。当id_colsvalue 的组合不能唯一标识观察时,结果将是一个命名列表。您可能会注意到,您有多个 Asian == 5 的组合,因此我决定使用所有剩余的列而不是 names_fromvalues_fromid_cols 中指定的列,因为这是默认选择只是region

    library(tidyr)
    
    test_df %>% 
      pivot_wider(names_from = code,
                  values_from = n) %>%
      arrange(region)
    
    # A tibble: 9 x 4
      company         region      positive negative
      <chr>           <chr>          <int>    <int>
    1 Google          Asia               4        5
    2 BlueOrigin      Asia               5        2
    3 Aliexpress      Asia               3        2
    4 Amazon          Europe             6        5
    5 Google          Europe             3        1
    6 Tencent         Europe             5        4
    7 SpaceX          Middle East        8        5
    8 Western Digital Middle East        6        5
    9 Aliexpress      Middle East        6        2
    

    【讨论】:

      【解决方案2】:

      使用reshape 的基本 R 选项

      reshape(
          test_df,
          direction = "wide",
          idvar = c("company", "region"),
          timevar = "code"
      )
      

      给予

                company      region n.positive n.negative
      1          Google        Asia          5          6
      2          Amazon      Europe          4          6
      3          SpaceX Middle East          2          2
      4      BlueOrigin        Asia          5          6
      5          Google      Europe          5          3
      6 Western Digital Middle East          2          4
      7      Aliexpress        Asia          6          3
      8         Tencent      Europe          5          3
      9      Aliexpress Middle East          4          4
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-03-16
        • 1970-01-01
        • 2018-06-04
        • 1970-01-01
        • 2023-03-21
        • 2020-07-30
        • 2019-09-09
        • 1970-01-01
        相关资源
        最近更新 更多