R:没有使用 gsub() 函数替换任何内容
R: Nothing was replaced using gsub() function
您好,我已尝试使用以下代码替换数据框中列的值。
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
}
您好,我已尝试使用以下代码替换数据框中列的值。
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
}