Pivot_wider 来自多个变量的函数(tidyr r 包)

Pivot_wider function (tidyr r package) from multiple variables

我想将数据框放在宽格式中,考虑两个变量作为标准(甚至可能是不必要的)。但是我对此发表评论是因为原始df是480行和几个子级别。

返回错误!

library(tidyr)
library(dplyr)
                                                                
df <- structure(list(ID = c(1, 2, 3, 4), Gender = c("Men", "Women", "Men", 
"Women"), Country = c("Austria", "Austria", "Austria", "Austria"
), Season_ID = c("2011", "2012", "2011", "2012"), Region_UN = c("A", 
"B", "A", "B")), row.names = c(NA, 
-4L), class = c("tbl_df", "tbl", "data.frame"))

df_wide <- df %>%
  pivot_wider(names_from = Gender,
              values_from = Region_UN,
              id_cols = c(Country, Season_ID))

警告信息: 值不是唯一标识的;输出将包含 list-cols.

我不知道我可以加入哪个参数 values_fn!

我们可以创建一个序列列

library(dplyr)
library(tidyr)
library(data.table)
df %>% 
  mutate(ID = NULL, rn = rowid(Country, Season_ID)) %>%     
  pivot_wider(names_from = Gender,
          values_from = Region_UN,
          id_cols = c(rn, Country, Season_ID))

也可以一起粘贴:

df_wide <- df %>%
  pivot_wider(names_from = Gender,
              values_from = Region_UN,
              id_cols = c(Country, Season_ID),
              values_fn = function(x) paste(x, collapse=","))

df_wide

并且两者也相同:

df_wide <- df %>%
  pivot_wider(names_from = Gender,
              values_from = Region_UN,
              id_cols = c(Country, Season_ID),
              values_fn = first)
df_wide