r - Paste values together based on a grouping variable -


i'm new @ r programing , im trying rearrange data frame. have column ids , column y string values. there's more 1 y per id multiple rows same id different y. want 1 row per id , y value concatenated in same cell in other column. there function ?

original data  id  y   apple b   pear c   grape   grape b   apple c   grape  transformed data    id   y      apple,grape   b    pear, apple   c    grape 

you can use aggregate here paste unique elements each idtogether

ggregate(y ~ id, unique(dat), paste, collapse = ", ") 

data

dat <- read.table(text="id  y   apple b   pear c   grape   grape b   apple c   grape", header=t) 

edit added collapse argument re @pdb comment , changed unique re @davidarenburg


Comments

Popular posts from this blog

css - SVG using textPath a symbol not rendering in Firefox -

Java 8 + Maven Javadoc plugin: Error fetching URL -

order - Notification for user in user account opencart -