合并R中的行与连接列中的内容的相同行名[duplicate]

时间:2021-12-18 16:52:26

This question already has an answer here:

这个问题在这里已有答案:

I need help merging the rows with the same name by concatenating the content in one of the columns. For example, in my dataframe,df, the rows with the same name match completely across the columns except in col 3. I want to merge the rows with the same rowname and concatenate the contents in col3 separated by a comma and get the result as shown below. Thank you for your help.

我需要帮助通过连接其中一列中的内容来合并具有相同名称的行。例如,在我的数据框df中,除了col 3之外,具有相同名称的行完全匹配列。我想将行与相同的rowname合并,并用colma连接col3中的内容并将结果作为如下所示。感谢您的帮助。

df

rowname  col1    col2  col3 
pat      122       A      T
bus      222       G      C
pat      122       A      G


result

rowname  col1    col2  col3 
pat      122       A      T,G
bus      222       G      C

1 个解决方案

#1


1  

Try

尝试

aggregate(col3~., df, FUN=toString)
#   rowname col1 col2 col3
#1     pat  122    A T, G
#2     bus  222    G    C

Or using dplyr

或者使用dplyr

library(dplyr)
 df %>%
    group_by_(.dots=names(df)[1:3]) %>%
    summarise(col3=toString(col3))
#  rowname col1 col2 col3
#1     bus  222    G    C
#2     pat  122    A T, G

data

df <- structure(list(rowname = c("pat", "bus", "pat"), col1 = c(122, 
222, 122), col2 = c("A", "G", "A"), col3 = c("T", "C", "G")), 
.Names = c("rowname", 
"col1", "col2", "col3"), row.names = c(NA, -3L), class = "data.frame")

#1


1  

Try

尝试

aggregate(col3~., df, FUN=toString)
#   rowname col1 col2 col3
#1     pat  122    A T, G
#2     bus  222    G    C

Or using dplyr

或者使用dplyr

library(dplyr)
 df %>%
    group_by_(.dots=names(df)[1:3]) %>%
    summarise(col3=toString(col3))
#  rowname col1 col2 col3
#1     bus  222    G    C
#2     pat  122    A T, G

data

df <- structure(list(rowname = c("pat", "bus", "pat"), col1 = c(122, 
222, 122), col2 = c("A", "G", "A"), col3 = c("T", "C", "G")), 
.Names = c("rowname", 
"col1", "col2", "col3"), row.names = c(NA, -3L), class = "data.frame")