Dynamic variable naming from dataframe columns
I have a two question test from a pool of several hundred questions. There are two question columns, and two response columns. I need to create new columns named by the question values and assign responses from the corresponding response values
A table of the data is as follows
question1 question2 response1 response2
SI089923 SI089801 B A
WK090824 WK090712 C B
WK091040 WK090843 D C
question1 <- c('SI089923','WK090824','WK091040')
question2 <- c('SI089801','WK090712','WK090843')
response1 <- c('B','C','D')
response2 <- c('A','B','C')
arrayQuestion <-paste( "question", 1:2, sep="")
arrayResponse <-paste( "response", 1:2, sep="")
I tried code found on this site but got an object not found error
for (i in 1:2){
varname <- eval(parse(text=paste(arrayQuestion[i], sep="")))
varvalue <- eval(parse(text=paste(arrayResponse[i], sep="")))
eval(parse(text=paste(varname, varvalue, sep=" <- ")), envir=.GlobalEnv)
}
Thanks in advance for your help
答案 0 :(得分:0)
为了澄清有关最终数据框架形式的问题,列名缩写为字符宽度为五:
ques1 <- c('SI083','WK094','WK090')
ques2 <- c('SI081','WK092','WK093')
resp1 <- c('B','C','D')
resp2 <- c('A','B','C')
初始数据框:
>ques1 ques2 resp1 resp2
>SI083 SI081 'B' 'A'
>WK094 WK092 'C' 'B'
>WK090 WK093 'D' 'C'
最终数据框:
>ques1 ques2 resp1 resp2 SI083 SI081 WK094 WK092 WK090 WK093
>SI083 SI081 'B' 'A' 'B' 'A'
>WK094 WK092 'C' 'B' 'C' 'B'
>WK090 WK093 'D' 'C' 'D' 'C'
答案 1 :(得分:0)
我找到了一个解决方案,虽然我不知道它是否是R中最有效的方法。该程序将读取包含变量名称列和变量值列的数据框,并动态创建基于变量的新列命名并将适用的变量值分配给新列。
names1 <- c('SI923','WK824','WK040') # vector of variable names
names2 <- c('SI801','WK712','WK843') # "
values1 <- c('B','C','D') # vector of variable values
values2 <- c('A','B','C') # "
require(reshape2)
dfOne <- data.frame(names1,names2,values1,values2)
print(dfOne)
以下是数据框的打印输出
names1 names2 values1 values2
1 SI923 SI801 B A
2 WK824 WK712 C B
3 WK040 WK843 D C
以下是编码新变量的代码
namesCol <-paste( "names", 1:2, sep="") # vector of data frame column names
valuesCol <-paste( "values", 1:2, sep="") # vector of data frame column names
for (j in 1:nrow(dfOne)){
for (i in 1:length(namesCol)){
namesCol.i<-eval(namesCol[i]) # data frame column name of variable names
varname<-eval(parse(text=paste(namesCol.i,'[j]',sep='')))
if (exists(varname)==FALSE){
eval(parse(text=paste('dfOne$',varname,'<- NA',sep='')))
}
valuesCol.i<-eval(valuesCol[i]) # data frame column name of variable values
varvalue<-eval(parse(text=paste(valuesCol.i,'[j]',sep='')))
if (is.character(varvalue)){
eval(parse(text=paste('dfOne$',varname,'[j]','<-',"'",varvalue,"'",sep='')))
}else{
eval(parse(text=paste('dfOne$',varname,'[j]','<-',varvalue,sep='')))
}
}
}
print(dfOne)
以下是带有新编码变量的数据框的打印输出
names1 names2 values1 values2 SI923 SI801 WK824 WK712 WK040 WK843
1 SI923 SI801 B A B A <NA> <NA> <NA> <NA>
2 WK824 WK712 C B <NA> <NA> C B <NA> <NA>
3 WK040 WK843 D C <NA> <NA> <NA> <NA> D C