Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 7th, 2012  |  syntax: None  |  size: 1.21 KB  |  hits: 15  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Pasting (or merging) two elements of a column together
  2. cpt <- c(23456,23456,10000,44555,44555)
  3. description <- c("tonsillectomy","tonsillectomy in >12 year old","brain transplant","castration","orchidectomy")
  4. cpt.desc <- data.frame(cpt,description)
  5.        
  6. cpt.wanted <- c(23456,10000,44555)
  7. description.wanted <- c("tonsillectomy; tonsillectomy in >12 year old","brain transplant","castration; orchidectomy")
  8. cpt.desc.wanted <- data.frame(cpt.wanted,description.wanted)
  9.        
  10. library("plyr")
  11. cpt.desc.wanted <- ddply(cpt.desc, .(cpt), summarise,
  12.   description.wanted = paste(unique(description), collapse="; "))
  13.        
  14. > cpt.desc.wanted
  15.     cpt                           description.wanted
  16. 1 10000                             brain transplant
  17. 2 23456 tonsillectomy; tonsillectomy in >12 year old
  18. 3 44555                     castration; orchidectomy
  19.        
  20. sapply( sapply(unique(cpt), function(x) grep(x, cpt) ),
  21.                        # creates sets of index vectors as a list
  22.         function(x) paste(description[x], collapse=";") )
  23.        # ... and this pastes each set of selected items from "description" vector
  24. [1] "tonsillectomy;tonsillectomy in >12 year old"
  25. [2] "brain transplant"                          
  26. [3] "castration;orchidectomy"