R语言 根据ID列组合合并多行

x33g5p2x  于 2023-10-13  发布在  其他
关注(0)|答案(2)|浏览(115)

我有以下的df:

df<- structure(list(X18.digit.contact.id = c("0034y00002kIZ3rAAG", 
"0034y00002kIZ3rAAG", "0034y00002kIZ3rAAG", "0034y00002PpX11AAF", 
"0034y00002PpX11AAF", "0034y00002PpX11AAF", "0034y00002jHjYKAA0", 
"0034y00002jHjYKAA0", "0034y00002jHjYKAA0"), `Fitness Goal` = c(2L, 
NA, NA, -1L, NA, NA, NA, 1L, NA), `Nutrition/Hydration Goal` = c(NA, 
NA, NA, NA, 0L, NA, 2L, NA, NA), `Lifestyle Goal` = c(NA, NA, 
2L, NA, NA, 0L, NA, NA, 1L)), class = c("grouped_df", "tbl_df", 
"tbl", "data.frame"), row.names = c(NA, -9L), groups = structure(list(
    X18.digit.contact.id = c("0034y00002PpX11AAF", "0034y00002jHjYKAA0", 
    "0034y00002kIZ3rAAG"), .rows = structure(list(4:6, 7:9, 1:3), ptype = integer(0), class = c("vctrs_list_of", 
    "vctrs_vctr", "list"))), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -3L), .drop = TRUE))

我希望基于X18 contactid列将这些行折叠在一起。因此,最终的df将由3行(3个唯一的ID)和4列(ID,健身目标,营养/水合作用,生活方式)组成。
我已经尝试了各种解决方案,我发现在这里从总结功能,

lapply: df2<- df[, lapply(.SD, paste0, collapse=""), by=X18.digit.contact.id]

但是,它们似乎都不起作用。我可以将其旋转更长时间,删除NA,然后再次将其旋转宽,这给了我想要的行格式,但是,它给出了以下警告消息,限制了我之后能够在列上执行的功能:

Values from `value` are not uniquely identified; output will contain list-cols.

注意:当我使用pivot_wider时,我可以通过为每个行分配一个唯一的行号来消除这个警告消息,但是,输出与df相同,其中的行没有按id变量分组。
任何帮助/见解与此将不胜感激。

byqmnocz

byqmnocz1#

在碱R中:

aggregate(.~X18.digit.contact.id, df, na.omit, na.action = identity)

  X18.digit.contact.id Fitness Goal Nutrition/Hydration Goal Lifestyle Goal
1   0034y00002jHjYKAA0            1                        2              1
2   0034y00002kIZ3rAAG            2                        1              2
3   0034y00002PpX11AAF           -1                        0              0
pivot_longer(df,-1, values_drop_na = TRUE)%>%
   pivot_wider()

# A tibble: 3 × 4
# Groups:   X18.digit.contact.id [3]
  X18.digit.contact.id `Fitness Goal` `Nutrition/Hydration Goal` `Lifestyle Goal`
  <chr>                         <int>                      <int>            <int>
1 0034y00002kIZ3rAAG                2                          1                2
2 0034y00002PpX11AAF               -1                          0                0
3 0034y00002jHjYKAA0                1                          2                1

在数据表中

dcast(na.omit(melt(df, 1)),X18.digit.contact.id~variable)
   X18.digit.contact.id Fitness Goal Nutrition/Hydration Goal Lifestyle Goal
1:   0034y00002PpX11AAF           -1                        0              0
2:   0034y00002jHjYKAA0            1                        2              1
3:   0034y00002kIZ3rAAG            2                        1              2
umuewwlo

umuewwlo2#

如果您使用data.table,可以尝试

> setDT(df)[, lapply(.SD, na.omit), X18.digit.contact.id]
   X18.digit.contact.id Fitness Goal Nutrition/Hydration Goal Lifestyle Goal
1:   0034y00002kIZ3rAAG            2                        1              2
2:   0034y00002PpX11AAF           -1                        0              0
3:   0034y00002jHjYKAA0            1                        2              1

由于您共享的数据已经分组,您只需运行

df %>%
    summarise(across(everything(), na.omit))

这给

X18.digit.contact.id `Fitness Goal` Nutrition/Hydration Goa…¹ `Lifestyle Goal`
  <chr>                         <int>                     <int>            <int>
1 0034y00002PpX11AAF               -1                         0                0
2 0034y00002jHjYKAA0                1                         2                1
3 0034y00002kIZ3rAAG                2                         1                2

相关问题