I have a dataframe (df) and am trying to add column z that contains a list of the qualitative elements from column y, but only the elements that are present when grouping the rows by column x.
df <- data.frame('x'=c("a","a","b","b"), 'y'=c("a","c","c","b"))
x y
1 a a
2 a c
3 b c
4 b b
#Desired outcome;
df <- data.frame(x,y,'z'=c("a,c", "a,c", "c,b", "c,b"))
x y z
1 a a a,c
2 a c a,c
3 b c c,b
4 b b c,b
I know there are a bunch of questions here on how to add/create new columns in a dataframe, but I couldn't find any involving subsetting. I was thinking of using the dplyr package and filter() or mutate(), or aggregating the elements with aggregate(), but have had no success. My attempts:
library(dplyr)
z <- for (i in row.names(df)) {
filter(df, x == unique(i))
df[ ,3] <- levels(df$y)
}
z <- aggregate(x = df, by = as.list(df$x), FUN = levels)
Much thanks.