[英]How to update the column-name without entering a string in R
我正在与WHO宏合作 ,将人体测量学参数转换为Z评分。
出于问题的目的,调用who2007
函数要求我们给出数据框的名称 ,然后才像ggplot函数中那样给出变量(列)的名称 。 问题在于,如果列名是Age
输入argument=Age
与输入argument='Age'
。 前者返回一个double
而后者返回一个list
。 我假设这是做df$Age
与df['Age']
的区别。
如果我只有列名的向量,并且每次都需要使用不同的列遍历相同的代码,那么如果我依次输入该字符向量的各个条目,则该函数会抛出错误,因为它遇到的是列表而不是列表内部加倍。 我该如何规避? 我能想到的一种方法是使用列号或使用任何grep方法来标识列号,但是还有另一种更好的方法吗?
附录
这是函数源代码(我认为其中一部分可以解释问题)
who2007 <- function(FileLab="Temp",FilePath="C:\\Documents and Settings",mydf,sex,age,weight,height,oedema=rep("n",dim(mydf)[1]),sw=rep(1,dim(mydf)[1])) {
#############################################################################
########### Calculating the z-scores for all indicators
#############################################################################
old <- options(warn=(-1))
sex.x<-as.character(get(deparse(substitute(mydf)))[,deparse(substitute(sex))])
age.x<-as.double(get(deparse(substitute(mydf)))[,deparse(substitute(age))])
weight.x<-as.double(get(deparse(substitute(mydf)))[,deparse(substitute(weight))])
height.x<-as.double(get(deparse(substitute(mydf)))[,deparse(substitute(height))])
if(!missing(oedema)) oedema.vec<-as.character(get(deparse(substitute(mydf)))[,deparse(substitute(oedema))]) else oedema.vec<-oedema
if(!missing(sw)) sw<-as.double(get(deparse(substitute(mydf)))[,deparse(substitute(sw))]) else sw<-as.double(sw)
sw<-ifelse(is.na(sw),0,sw)
sex.vec<-NULL
sex.vec<-ifelse(sex.x!="NA" & (sex.x=="m" | sex.x=="M" | sex.x=="1"),1,ifelse(sex.x!="NA" & (sex.x=="f" | sex.x=="F" | sex.x=="2"),2,NA))
age.vec<-age.x
height.vec<-height.x
oedema.vec<-ifelse(oedema.vec=="n" | oedema.vec=="N","n",ifelse(oedema.vec=="y" | oedema.vec=="Y","y","n"))
mat<-cbind.data.frame(age.x,as.double(sex.vec),weight.x,height.x,oedema.vec,sw,stringsAsFactors=F)
names(mat)<-c("age.mo","sex","weight","height","oedema","sw")
mat$cbmi<-mat$weight/((height.vec/100)^2)
mat$zhfa<-NULL
mat$fhfa<-NULL
mat$zwfa<-NULL
mat$fwfa<-NULL
mat$zbfa<-NULL
mat$fbfa<-NULL
#############################################################################
########### Calculating the z-scores for all indicators
#############################################################################
cat("Please wait while calculating z-scores...\n")
### Height-for-age z-score
mat<-calc.zhfa(mat,hfawho2007)
### Weight-for-age z-score
mat<-calc.zwei(mat,wfawho2007)
### BMI-for-age z-score
mat<-calc.zbmi(mat,bfawho2007)
#### Rounding the z-scores to two decimals
mat$zhfa<-rounde(mat$zhfa,digits=2)
mat$zwfa<-rounde(mat$zwfa,digits=2)
mat$zbfa<-rounde(mat$zbfa,digits=2)
#### Flagging z-score values for individual indicators
mat$fhfa<-ifelse(abs(mat$zhfa) > 6,1,0)
mat$fwfa<-ifelse(mat$zwfa > 5 | mat$zwfa < (-6),1,0)
mat$fbfa<-ifelse(abs(mat$zbfa) > 5,1,0)
if(is.na(mat$age.mo) & mat$oedema=="y") {
mat$fhfa<-NA
mat$zwfa<-NA
mat$zbfa<-NA
}
mat<-cbind.data.frame(mydf,mat[,-c(2:6)])
附录2
该脚本还旨在由最终用户运行,因为这些用户可能无法修改其源代码。 有没有一种方法不需要修改函数源代码?
我们可以测试输入数据框是否具有必需的列,然后摆脱“ deparse get”步骤,例如:
who2007 <- function(FileLab = "Temp", FilePath = "C:\\Documents and Settings",
mydf,
oedema = rep("n",dim(mydf)[1]),sw=rep(1,dim(mydf)[1])) {
if(!all(c("sex", "age", "weight", "height") %in% colnames(mydf))) stop("mydf, must have 'sex', 'age', 'weight', 'height' columns")
sex.x <- mydf$sex
age.x <- mydf$age
# ...
# some code
# ...
#return
list(sex.x, age.x)
}
测试:
#example dataframe
x <- head(mtcars)
# this errors as required columns are missing
who2007(mydf = x)
# Error in who2007(mydf = x) :
# mydf, must have 'sex', 'age', 'weight', 'height' columns
# now update columns with required column names, and it works fine:
colnames(x)[1:4] <- c("sex", "age", "weight", "height")
who2007(mydf = x)
# [[1]]
# [1] 21.0 21.0 22.8 21.4 18.7 18.1
#
# [[2]]
# [1] 6 6 4 6 8 6
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.