嗨,我尝试使用以下代码替换数据框中的列值。
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)
关于此问题的任何想法。
答案 0 :(得分:1)
您正在向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
}