我有两个临床程序结算信息来源,我已加在一起(rbind
)。在每一行中都有一个CPT字段和一个提供简要说明的CPT.description字段。但是,描述与两个来源略有不同。我希望能够将它们结合起来。这样,如果使用不同的单词或缩写,那么我可以只进行字符串搜索以找到我要查找的内容。
因此,让我们构建一个我能够生成的数据表的简化表示。
cpt <- c(23456,23456,10000,44555,44555)
description <- c("tonsillectomy","tonsillectomy in >12 year old","brain transplant","castration","orchidectomy")
cpt.desc <- data.frame(cpt,description)
这就是我想要达到的目标。
cpt.wanted <- c(23456,10000,44555)
description.wanted <- c("tonsillectomy; tonsillectomy in >12 year old","brain transplant","castration; orchidectomy")
cpt.desc.wanted <- data.frame(cpt.wanted,description.wanted)
我尝试过使用诸如unstack之类的函数然后lapply(list,paste),但这并没有粘贴每个列表的元素。我也尝试过重塑,但没有明确的变量来区分第一版或第二版的描述,甚至在某些情况下也不是第三版。真正烦人的部分是几个月或几年前我遇到了类似的问题,有人在stackoverflow或r-help帮助我,而在我的生活中我找不到它。
所以潜在的问题是,想象一下我面前有一个电子表格。我需要对两个甚至三个描述单元进行垂直合并(粘贴),这些单元在相邻列中具有相同的CPT代码。
我应该使用什么流行语来搜索此问题的解决方案。 非常感谢你的帮助。
答案 0 :(得分:2)
sapply( sapply(unique(cpt), function(x) grep(x, cpt) ),
# creates sets of index vectors as a list
function(x) paste(description[x], collapse=";") )
# ... and this pastes each set of selected items from "description" vector
[1] "tonsillectomy;tonsillectomy in >12 year old"
[2] "brain transplant"
[3] "castration;orchidectomy"
答案 1 :(得分:1)
以下是使用plyr
的方法。
library("plyr")
cpt.desc.wanted <- ddply(cpt.desc, .(cpt), summarise,
description.wanted = paste(unique(description), collapse="; "))
给出了
> cpt.desc.wanted
cpt description.wanted
1 10000 brain transplant
2 23456 tonsillectomy; tonsillectomy in >12 year old
3 44555 castration; orchidectomy