0

i have the following example data frame in R:

ID = c(rep(1,4),rep(2,3),rep(3,2),4,5);ID
VAR = c(rep("A",3),"B",rep("C",2),"D",rep("E",2),"F","G");VAR
D = data.frame(ID,VAR);D

My purpose is to construct a separate variable outside the data frame that will search all the ids and print the combined texts related to each id. For example the data frame

ID VAR
1 A
1 A
1 A
1 B
2 C
2 C
2 D
3 E
2 E
4 F
5 G

ideal output given the above data frame in R will be :

ID TEXTS
1 A,B
2 C,D
3 E
4 F
5 G

Any help ?

ThomasIsCoding
  • 96,636
  • 9
  • 24
  • 81
Homer Jay Simpson
  • 1,043
  • 6
  • 19

1 Answers1

1

You can try this

> aggregate(. ~ ID, unique(D), c)
  ID  VAR
1  1 A, B
2  2 C, D
3  3    E
4  4    F
5  5    G

or

> aggregate(. ~ ID, unique(D), toString)
  ID  VAR
1  1 A, B
2  2 C, D
3  3    E
4  4    F
5  5    G
ThomasIsCoding
  • 96,636
  • 9
  • 24
  • 81