user3243478 user3243478 - 1 month ago 16
R Question

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

Answer

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 
Comments