steven
steven

Reputation: 683

Convert multiple rows to single row in R

Original table:

ID------REMARK

1------ A

2------ B

1-------AG

3-------V

2-------BS

1--------E

4--------B

4--------BS

Required table:

ID......REMARK

1-------A,AG,E

2-------B,BS

3-------V

4-------B,BS

and then list according to frequently occurring sequences:

REMARK......OCCURRENCES


A,AG,E-------1

B,BS---------2

V -----------1

Upvotes: 0

Views: 802

Answers (2)

Beasterfield
Beasterfield

Reputation: 7113

Here is a plyr solution:

library(plyr)
dt.agg <- ddply( dt, .(ID), summarise, Remark = paste( REMARK, collapse = ",", sep = "" )  )
ddply( dt.agg, .(Remark), nrow )

  Remark V1
1 A,AG,E  1
2   B,BS  2
3      V  1

Upvotes: 1

Sven Hohenstein
Sven Hohenstein

Reputation: 81683

Here's an approach. dat is the name of your data frame:

res1 <- aggregate(REMARK ~ ID, dat, paste, collapse = ",")
#   ID REMARK
# 1  1 A,AG,E
# 2  2   B,BS
# 3  3      V
# 4  4   B,BS

table(res1$REMARK)
# 
# A,AG,E   B,BS      V 
#      1      2      1 

Upvotes: 3

Related Questions