user969113
user969113

Reputation: 2429

R: Concatenate strings with same values

I have a dataframe like the following:

1   a
2   a
3   a
4   a
a   a
x   b
y   b
b   b

How can I use are to get something like:

a   1,2,3,4
b   x,y

Consider that the identifier a and b are also listed in column 1 but should not be part of the concatenated string. Many thanks!

Upvotes: 1

Views: 554

Answers (1)

akrun
akrun

Reputation: 887951

Based on the initial description,

aggregate(V1~V2, df1, toString)
#  V2         V1
#1  a 1, 2, 3, 4
#2  b       x, y

Update

Suppose, if we need to remove the elements in first column ('V1') that are also present in the grouping column ('V2') before we paste the elements of V1 together, a couple of options are listed below:

library(data.table)
setDT(df2)[!V1 %chin% V2, toString(V1), by=V2]
#    V2         V1
#1:  a 1, 2, 3, 4
#2:  b       x, y

Or

 library(dplyr)   
 df2 %>%
     filter(!V1 %in% V2) %>% 
     group_by(V2) %>%
     summarise(V1=toString(V1))

data

df1 <- structure(list(V1 = c("1", "2", "3", "4", "x", "y"),
 V2 = c("a", 
 "a", "a", "a", "b", "b")), .Names = c("V1", "V2"), 
class = "data.frame", row.names = c(NA, -6L))

df2 <- structure(list(V1 = c("1", "2", "3", "4", "a", "x", "y", "b"), 
 V2 = c("a", "a", "a", "a", "a", "b", "b", "b")), .Names = c("V1", 
"V2"), class = "data.frame", row.names = c(NA, -8L))

Upvotes: 1

Related Questions