记录心得,以防遗忘;重拾笔记,以备所需。
----如果我的心得笔记对你有些许帮助,求顺手点赞哈~
R匹配函数pmatch
tumor_data[,pmatch(clin_data[,1],colnames(tumor_data),nomatch = 0)]
注意啊,后面的才是你要留下的目标index!如果搞反了就会导致返回结果不正确,报错“超区间”还好,没报错继续往下做才是真的大坑!下面举个例子:
(1)一不小心少掉数据系列:
tumor <- paste(rep("tumor",55),1:55)
clin <- paste(rep("tumor",66),c(1:33,56:66,34:55))
clin [pmatch(tumor ,clin ,nomatch = 0)]
length(clin [pmatch(tumor ,clin ,nomatch = 0)])
tumor [pmatch(tumor ,clin ,nomatch = 0)]
length(which(tumor [pmatch(tumor ,clin ,nomatch = 0)] != 0))
结果展示:
就酱紫漏掉11个样本了【手动滑稽】
(2)报错系列:
tumor <- paste(rep("tumor",22),1:22)
clin <- paste(rep("tumor",35),c(1:6,23:35,12:34))
tumor <- as.matrix(tumor )
tumor [pmatch(tumor ,clin),]
tumor [pmatch(clin,tumor ),]
结果展示:
报错类型:
Error in tumor[pmatch(tumor, clin), ] : 下标出界
R语言匹配函数,下次遇到新坑的话,还是会在这里补上~
也欢迎小伙伴在评论区分享自己遇到的问题~