繁体   English   中英

在 R 的两列上使用 pivot_wide() 从宽转换为长

[英]Converting from wide to long, using pivot_wide() on two columns in R

我想通过两列中的值将我的数据从长格式转换为宽格式。 如何使用tidyverse做到这一点?

更新dput

structure(list(Country = c("Algeria", "Benin", "Ghana", "Algeria", 
                       "Benin", "Ghana", "Algeria", "Benin", "Ghana"
), Indicator = c("Indicator 1", 
             "Indicator 1", 
             "Indicator 1", 
             "Indicator 2", 
             "Indicator 2", 
             "Indicator 2",
             "Indicator 3", 
             "Indicator 3", 
             "Indicator 3"
), Status = c("Actual", "Forecast", "Target", "Actual", "Forecast", 
          "Target", "Actual", "Forecast", "Target"), Value = c(34, 15, 5, 
                                                               28, 5, 2, 43, 5, 
                                                               1)), row.names 
= c(NA, -9L), class = c("tbl_df", "tbl", "data.frame"))


    Country Indicator   Status   Value
    <chr>   <chr>       <chr>    <dbl>
1 Algeria Indicator 1 Actual      34
2 Benin   Indicator 1 Forecast    15
3 Ghana   Indicator 1 Target       5
4 Algeria Indicator 2 Actual      28
5 Benin   Indicator 2 Forecast     5
6 Ghana   Indicator 2 Target       2
7 Algeria Indicator 3 Actual      43
8 Benin   Indicator 3 Forecast     5
9 Ghana   Indicator 3 Target       1

预期 output

Country Indicator1_Actual Indicator1_Forecast Indicator1_Target Indicator2_Actual

Algeria       34                    15                 5           28

ETC

感谢任何提示!

foo <- data %>% pivot_wider(names_from = c("Indicator","Status"), values_from = "Value") 

完美运行!

我认为错误在于您的pivot_wider()命令data %>% pivot_wider(names_from = Indicator, values_from = c(Indicator, Status))

我敢打赌,您不能将同一列用于名称和值。

试试这个代码

data %>% pivot_wider(names_from = c(Indicator, Status), values_from = Value))

说明:由于您希望列名称Indicator 1_Actual ,因此您需要将列 indicator 和 status 都放入您的names_from

如果您提供示例数据并预期 output,将会很有帮助。 但是我在我的虚拟数据上对此进行了测试,它给出了预期的 output -

数据:

# A tibble: 4 x 4
     a1    a2 a3       a4
  <int> <int> <chr> <dbl>
1     1     5 s        10
2     2     4 s        20
3     3     3 n        30
4     4     2 n        40

调用: a %>% pivot_wider(names_from = c(a2, a3), values_from = a4)

Output:

# A tibble: 4 x 5
     a1 `5_s` `4_s` `3_n` `2_n`
  <int> <dbl> <dbl> <dbl> <dbl>
1     1    10    NA    NA    NA
2     2    NA    20    NA    NA
3     3    NA    NA    30    NA
4     4    NA    NA    NA    40

如果要复制,请在此处提供数据

structure(list(a1 = 1:4, a2 = 5:2, a3 = c("s", "s", "n", "n"), 
    a4 = c(10, 20, 30, 40)), row.names = c(NA, -4L), class = c("tbl_df", 
"tbl", "data.frame"))

编辑:对于尝试正确的pivot_wider()命令后编辑的问题 - 看起来您的数据实际上可能有重复,在这种情况下,您看到的 output 会有意义 - 我建议您尝试弄清楚您的数据是否真的使用filter(Country ==.., Indicator ==.., Status ==..)

这可以通过在pivot_wider()names_from参数中将两个列都调用为 pivot 来实现。

data %>% 
  pivot_wider(names_from = c("Indicator","Status"),
              values_from = "Value")

结果

  Country `Indicator 1_Ac… `Indicator 1_Fo… `Indicator 1_Ta… `Indicator 2_Ac… `Indicator 2_Fo…
  <chr>              <dbl>            <dbl>            <dbl>            <dbl>            <dbl>
1 Algeria               34               15                5               28                5

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM