在 tidyr 中使用 'gather' 函数时遇到问题
Having trouble using the 'gather' function in tidyr
我在使用 R 中的 gather 函数时遇到问题。
这是示例数据框 -
library(dplyr)
library(tidyr)
DF = data.frame(Region = c("Asia", "Asia", "Asia", "Europe", "Europe"),
`Indicator Name` = c("Population", "GDP", "GNI", "Population", "GDP"),
`2004` = c(22, 33,44,55,56),
`2005` =c(223, 44,555,66,64))
Region Indicator.Name X2004 X2005
1 Asia Population 22 223
2 Asia GDP 33 44
3 Asia GNI 44 555
4 Europe Population 55 66
5 Europe GDP 56 64
这就是我想要的数据框
DF2 = data.frame(Region = c("Asia", "Asia", "Europe", "Europe"),
Year = c("X2004", "X2005"),
population = c(22, 224, 55, 66),
GDP = c(33, 44, 56,64))
Region Year population GDP
1 Asia X2004 22 33
2 Asia X2005 224 44
3 Europe X2004 55 56
4 Europe X2005 66 64
我想通过 tidyr
中的 gather
函数执行此操作。
我不知道该怎么做。这是我试过的 -
gather(DF, key= DF$Indicator.Name, values = "values")
这不是一个简单的gather
函数。首先,您需要使数据框变长,然后使其变宽以切换所需的列。
这是使用新的 pivot_longer
和 pivot_wider
函数的解决方案。
library(dplyr)
library(tidyr)
DF = data.frame(Region = c("Asia", "Asia", "Asia", "Europe", "Europe"),
`Indicator Name` = c("Population", "GDP", "GNI", "Population", "GDP"),
`2004` = c(22, 33,44,55,56),
`2005` =c(223, 44,555,66,64))
DF %>% pivot_longer(cols = starts_with("x")) %>%
pivot_wider(names_from = Indicator.Name, values_from = value)
# A tibble: 4 x 5
Region name Population GDP GNI
<fct> <chr> <dbl> <dbl> <dbl>
1 Asia X2004 22 33 44
2 Asia X2005 223 44 555
3 Europe X2004 55 56 NA
4 Europe X2005 66 64 NA
使用 gather
和 spread
,您有:
DF %>%
gather(-Indicator.Name, -Region, key= "Year", value = "value") %>%
spread(Indicator.Name, value)
我在使用 R 中的 gather 函数时遇到问题。 这是示例数据框 -
library(dplyr)
library(tidyr)
DF = data.frame(Region = c("Asia", "Asia", "Asia", "Europe", "Europe"),
`Indicator Name` = c("Population", "GDP", "GNI", "Population", "GDP"),
`2004` = c(22, 33,44,55,56),
`2005` =c(223, 44,555,66,64))
Region Indicator.Name X2004 X2005
1 Asia Population 22 223
2 Asia GDP 33 44
3 Asia GNI 44 555
4 Europe Population 55 66
5 Europe GDP 56 64
这就是我想要的数据框
DF2 = data.frame(Region = c("Asia", "Asia", "Europe", "Europe"),
Year = c("X2004", "X2005"),
population = c(22, 224, 55, 66),
GDP = c(33, 44, 56,64))
Region Year population GDP
1 Asia X2004 22 33
2 Asia X2005 224 44
3 Europe X2004 55 56
4 Europe X2005 66 64
我想通过 tidyr
中的 gather
函数执行此操作。
我不知道该怎么做。这是我试过的 -
gather(DF, key= DF$Indicator.Name, values = "values")
这不是一个简单的gather
函数。首先,您需要使数据框变长,然后使其变宽以切换所需的列。
这是使用新的 pivot_longer
和 pivot_wider
函数的解决方案。
library(dplyr)
library(tidyr)
DF = data.frame(Region = c("Asia", "Asia", "Asia", "Europe", "Europe"),
`Indicator Name` = c("Population", "GDP", "GNI", "Population", "GDP"),
`2004` = c(22, 33,44,55,56),
`2005` =c(223, 44,555,66,64))
DF %>% pivot_longer(cols = starts_with("x")) %>%
pivot_wider(names_from = Indicator.Name, values_from = value)
# A tibble: 4 x 5
Region name Population GDP GNI
<fct> <chr> <dbl> <dbl> <dbl>
1 Asia X2004 22 33 44
2 Asia X2005 223 44 555
3 Europe X2004 55 56 NA
4 Europe X2005 66 64 NA
使用 gather
和 spread
,您有:
DF %>%
gather(-Indicator.Name, -Region, key= "Year", value = "value") %>%
spread(Indicator.Name, value)