您好我已尝试使用以下代码替换数据框中的列值。
sampleNumber <- c(1:length(sampleId))
for (value in sampleNumber){
genotypeCol <- paste("annotateData$", sampleId[value], sep = "") #sampleId is a vector contains column names in annotateData
genotypeCol <- gsub("0\\/0", "ref", genotypeCol)
genotypeCol <- gsub("0\\/1|0\\/2|0\\/3|1\\/2|1\\/3|2\\/3", "het", genotypeCol)
genotypeCol <- gsub("1\\/1|2\\/2|3\\/3", "hom", genotypeCol)
}
无论如何,内容仍然相同,但如果我使用以下代码,它可以正常工作。
annotateData$Genotype_SM01 <- gsub("0\\/0", "ref", annotateData$Genotype_SM01)
annotateData$Genotype_SM01 <- gsub("0\\/0", "ref", annotateData$Genotype_SM01)
annotateData$Genotype_SM01 <- gsub("0\\/0", "ref", annotateData$Genotype_SM01)
所以对于这个问题的任何想法。
您正在使用包含变量名称的字符串来输入gsub()
。要获取实际变量,请使用get(paste("annotateData$", sampleId[value], sep = ""))
编辑
aux=get("annotateData")
var=aux[,sampleID[value]]
在这种情况下,var
持有annotateData$Genotype_SM01
的值
编辑2 重新处理您的问题,以下代码应该做你想要的。
annotateData=data.frame("Genotype_SM01"=c("a","a","b"),
"Genotype_SM02"=c("a","a","a"),
"Genotype_SM02"=c("b","b","a"),
stringsAsFactors = FALSE)
sampleId=names(annotateData)
sampleNumber <- c(1:length(sampleId))
for (value in sampleNumber){
aux=annotateData[,sampleID[value]]
aux <- gsub("0\\/0", "ref", aux)
aux <- gsub("0\\/1|0\\/2|0\\/3|1\\/2|1\\/3|2\\/3", "het", aux)
aux <- gsub("1\\/1|2\\/2|3\\/3", "hom", aux)
annotateData[,sampleID[value]]=aux
}