簡體   English   中英

dplyr 合並行並合並除一個之外的所有列匹配的列

[英]dplyr merge rows and combine column where all columns except one match

我有一個如下所示的 df:

ID  Val1 Val2 Val3
0     2    3    4
1     5    3    2
2     3    4    3
3     4    5    9
3     2    5    9

除 Val1 外,所有值都將匹配,因此我希望合並存在重復 ID 的行,並合並與 Val1 不同的值。 我預期的 output 將是:

ID  Val1 Val2 Val3
0     2    3    4
1     5    3    2
2     3    4    3
3    4,2   5    9

aggregate function 似乎很接近,但不完全是我想要的。

我們可以按“ID”分組並通過獲取主題的unique值並包裝在list中來總結其他列

library(dplyr)
df1 %>%
    group_by(ID) %>% 
    summarise_all(~ list(unique(.)))

或者如果我們需要單個字符串,可以paste unique元素

library(stringr)
df1 %>%
   group_by(ID) %>%
   summarise_all(~ toString(unique(.)))
# A tibble: 4 x 4
#     ID Val1  Val2  Val3 
#  <int> <chr> <chr> <chr>
#1     0 2     3     4    
#2     1 5     3     2    
#3     2 3     4     3    
#4     3 4, 2  5     9    

base R中,我們可以使用aggregate

aggregate(.~ ID, df1, I) # creates list column
aggregate(.~ ID, df1, toString) # creates string

數據

df1 <- structure(list(ID = c(0L, 1L, 2L, 3L, 3L), Val1 = c(2L, 5L, 3L, 
4L, 2L), Val2 = c(3L, 3L, 4L, 5L, 5L), Val3 = c(4L, 2L, 3L, 9L, 
9L)), class = "data.frame", row.names = c(NA, -5L))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM