0

I am struggling with this,

I would like to change this long data frame:

longdf <- data.frame(visitId = c("a", "b", "b", "c"),icd9 = c("441","4424", "443", "441"))

   visitId  icd9
1       a   441
2       b   4424
3       b   443
4       c   441

To this form,

  visitId  icd9
1       a   c(441)
2       b   c(4424,443)
3       c   c(441)

So that I can write a grep statement using dplyr on the individual rows. Help would be appreciated.

Avinash Raj
  • 172,303
  • 28
  • 230
  • 274

1 Answers1

0

Below is few quick ways to achieve concatenating values of data frame.

Option 1:
# Alternative to paste (@ Richard Scriven), you can use toString
aggregate(icd9 ~ visitId, longdf, toString)

# You can also specify desired separator in collapse
aggregate(icd9 ~ visitId, paste,collapse=",",data=longdf)

Option 2:
# Using plyr, you can specify desired the separator in collapse
library(plyr)
ddply(longdf, .(visitId), summarize, rnames = paste(icd9, collapse = ","))

Option 3:
# Using ddply, you can specify desired the separator in collapse
library(dplyr)
longdf %>% group_by(visitId) %>% summarise (rnames = paste(icd9,collapse =     ","))
# Output
#  visitId   rnames
#1       a      441
#2       b 4424,443
#3       c      441

Hope this helps.

Manohar Swamynathan
  • 2,065
  • 21
  • 23