-4

I am a beginner in R. I have this kind of data, data1

And I wanted to have a data like this: data2

I have found examples of people removing duplicates and combine the some data as list. but majority of examples only consist of 2 columns.

Hope anyone can help me

NAA
  • 1

1 Answers1

1

This can be done using group by operations. We group by the common columns and paste the elements in the column of interest. In the example, it is "X". Group by summarise can be done using data.table, dplyr, aggregate (from base R) etc.

With data.table, we convert the 'data.frame' to 'data.table' (setDT(df1)), grouped by multiple columns (here I used names to specify multiple columns), and paste the "X" column elements.

library(data.table)
setDT(df1)[, list(X= toString(X)), c(names(df1)[c(1:7,9)])]

Or using the same method with dplyr.

library(dplyr) 
df1 %>%
   group_by_(.dots= names(df1)[c(1:7,9)]) %>%
   summarise(X= toString(X))
akrun
  • 874,273
  • 37
  • 540
  • 662
  • thank you very much for the response. But, how if i have much larger dataset that have 3 replicates and want to list all the three in X column (eg. GV,MI, MII)? – NAA Jan 12 '16 at 04:48
  • @NAA The code will paste all those cases. – akrun Jan 12 '16 at 05:04