从长转换为宽,在 R 中的两列上使用 pivot_wide()
Converting from long to wide, 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
预期输出
Country Indicator1_Actual Indicator1_Forecast Indicator1_Target Indicator2_Actual
Algeria 34 15 5 28
等等
感谢任何提示!
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))
说明:由于您希望 列名称 成为 指标 1_Actual,因此您需要指标和状态都在进行中进入你的 names_from
如果您提供示例数据和预期输出,将会很有帮助。但我在我的虚拟数据上对此进行了测试,它给出了预期的输出 -
数据:
# 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)
输出:
# 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()
命令后编辑的问题 - 看起来您的数据实际上可能有重复项,在这种情况下,您看到的输出是有意义的 - 我建议您尝试使用 filter(Country == .., Indicator == .., Status == ..)
来确定您的数据是否真的有重复项
这可以通过在 pivot_wider()
.
的 names_from
参数中调用你的两列以更宽的旋转来实现
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
我想通过两列中的值将我的数据从长格式转换为宽格式。我如何使用 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
预期输出
Country Indicator1_Actual Indicator1_Forecast Indicator1_Target Indicator2_Actual
Algeria 34 15 5 28
等等
感谢任何提示!
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))
说明:由于您希望 列名称 成为 指标 1_Actual,因此您需要指标和状态都在进行中进入你的 names_from
如果您提供示例数据和预期输出,将会很有帮助。但我在我的虚拟数据上对此进行了测试,它给出了预期的输出 -
数据:
# 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)
输出:
# 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()
命令后编辑的问题 - 看起来您的数据实际上可能有重复项,在这种情况下,您看到的输出是有意义的 - 我建议您尝试使用 filter(Country == .., Indicator == .., Status == ..)
这可以通过在 pivot_wider()
.
names_from
参数中调用你的两列以更宽的旋转来实现
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