Question

I now have a data look like this:

V1  V2  V3  V4
a   1   2  name1
b   3   4  name2
b   3   4  name3
c   2   5  name4

and I want to get this result by grouping based on V1,V2,V3 using sqldf

V1  V2  V3  V4
a   1   2  name1
b   3   4  name2+name3
c   2   5  name4

I am thinking to use sth like

sqldf("select *, paste(V4) as V5 from table group by V1,V2,V3")

but I have trouble finding the right function to put as "paste" above. I wrote a complicated loop to solve this problem, but I am wondering if there is a simple way. Could someone help me out? Any input would be very appreciated! Thank you for your time!

Thanks, Raine

Was it helpful?

Solution

Try this:

sqldf("select V1, V2, V3, group_concat(V4) V4 from DF group by V1, V2, V3")

giving:

  V1 V2 V3          V4
1  a  1  2       name1
2  b  3  4 name2,name3
3  c  2  5       name4
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top