Reputation: 13
Hi I've tried to replace value of columns in data frame using the following code.
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)
}
Anyway, the content are still the same, but it works fine if I use the following code instead.
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)
So any ideas to for this issue.
Upvotes: 1
Views: 49
Reputation: 2467
You are feeding gsub()
with a character string containing your variable name. To get the actual variable , use get(paste("annotateData$", sampleId[value], sep = ""))
Edit
aux=get("annotateData")
var=aux[,sampleID[value]]
In this case, var
holds the value of annotateData$Genotype_SM01
edit 2
reworking your problem, the following code should do what you want.
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
}
Upvotes: 1