我正在努力解决这个问题,
我想更改这个长数据框:
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
到这个表格,
visitId icd9
1 a c(441)
2 b c(4424,443)
3 c c(441)
这样我就可以在各行上使用dplyr编写一个grep语句。 帮助将不胜感激。
答案 0 :(得分:0)
以下几种快速实现数据帧连接值的方法。
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
希望这有帮助。