R重塑数据集

时间:2020-01-31 17:08:14

标签: r

我正在尝试使用R重塑形状以扩展整个数据集,这是代码,我想拥有hashCode,但是我有点挣扎。

df2
value <- seq(1,20,1)
country <- c("AT","AT","AT","AT",
             "BE","BE","BE","BE",
             "CY","CY","CY", "CY",
             "DE","DE","DE","DE",
             "EE", "EE","EE","EE")

df <- data.frame(country, value)
df

#    country value
# 1       AT     1
# 2       AT     2
# 3       AT     3
# 4       AT     4
# 5       BE     5
# 6       BE     6
# 7       BE     7
# 8       BE     8
# 9       CY     9
# 10      CY    10
# 11      CY    11
# 12      CY    12
# 13      DE    13
# 14      DE    14
# 15      DE    15
# 16      DE    16
# 17      EE    17
# 18      EE    18
# 19      EE    19
# 20      EE    20

有帮助吗?

2 个答案:

答案 0 :(得分:2)

我们可以使用pivot_wider

重塑为“宽”格式
library(dplyr)
library(tidyr)
df %>% 
  group_by(country)  %>% 
  mutate(rn = row_number()) %>%
  pivot_wider(names_from = country, values_from = value)
# A tibble: 4 x 6
#     rn    AT    BE    CY    DE    EE
#  <int> <dbl> <dbl> <dbl> <dbl> <dbl>
#1     1     1     5     9    13    17
#2     2     2     6    10    14    18
#3     3     3     7    11    15    19
#4     4     4     8    12    16    20

或使用base R

out <- unstack(df, value ~ country)
str(out)
#'data.frame':  4 obs. of  5 variables:
# $ AT: num  1 2 3 4
# $ BE: num  5 6 7 8
# $ CY: num  9 10 11 12
# $ DE: num  13 14 15 16
# $ EE: num  17 18 19 20

答案 1 :(得分:2)

使用tidyversedplyrtidyr

df %>% group_by(country) %>% 
  mutate(row=row_number()) %>% 
  pivot_wider(names_from = country,values_from=value)
# A tibble: 4 x 6
    row    AT    BE    CY    DE    EE
  <int> <dbl> <dbl> <dbl> <dbl> <dbl>
1     1     1     5     9    13    17
2     2     2     6    10    14    18
3     3     3     7    11    15    19
4     4     4     8    12    16    20