Is there any way to use string stored in variable as a column name in a new data frame? The expected result should be:
有没有办法使用存储在变量中的字符串作为新数据框中的列名?预期结果应该是:
col.name <- 'col1'
df <- data.frame(col.name=1:4)
print(df)
# Real output
col.name
1 1
2 2
3 3
4 4
# Expected output
col1
1 1
2 2
3 3
4 4
I'm aware that I can create data frame and then use names() to rename column or use df[, col.name] for existing object, but I'd like to know if there is any other solution which could be used during creating data frame.
我知道我可以创建数据框,然后使用names()重命名列或使用df [,col.name]作为现有对象,但我想知道是否有任何其他解决方案可以在使用过程中使用创建数据框架。
3 个解决方案
#1
16
You cannot pass a variable into the name of an argument like that.
您不能将变量传递给这样的参数名称。
Instead what you can do is:
相反,你可以做的是:
df <- data.frame(placeholder_name = 1:4)
names(df)[names(df) == "placeholder_name"] <- col.name
or use the default name of "V1"
:
或使用默认名称“V1”:
df <- data.frame(1:4)
names(df)[names(df) == "V1"] <- col.name
or assign by position:
或按职位分配:
df <- data.frame(1:4)
names(df)[1] <- col.name
or if you only have one column just replace the entire names
attribute:
或者,如果您只有一列,则只需替换整个名称属性:
df <- data.frame(1:4)
names(df) <- col.name
There's also the set_names
function in the magrittr
package that you can use to do this last solution in one step:
magrittr包中还有set_names函数,您可以使用它来一步完成最后一个解决方案:
library(magrittr)
df <- set_names(data.frame(1:4), col.name)
But set_names
is just an alias for:
但是set_names只是一个别名:
df <- `names<-`(data.frame(1:4), col.name)
which is part of base R. Figuring out why this expression works and makes sense will be a good exercise.
这是基础R的一部分。弄清楚为什么这个表达有效并且有意义将是一个很好的练习。
#2
3
In addition to ssdecontrol's answer, there is a second option.
除了ssdecontrol的答案,还有第二种选择。
You're looking for mget
. First assign the name to the variable, then the value to the variable that you have previously assigned. After that, mget will evaluate the string and pass it to data.frame.
你在寻找mget。首先将名称分配给变量,然后将值赋给先前分配的变量。之后,mget将评估字符串并将其传递给data.frame。
assign(col.name, "col1")
assign(paste(col.name), 1:4)
df <- data.frame(mget(col.name))
print(df)
col1
1 1
2 2
3 3
4 4
#3
0
I don't recommend you do this, but:
我不建议你这样做,但是:
col.name <- 'col1'
eval(parse(text=paste0('data.frame(', col.name, '=1:4)')))
#1
16
You cannot pass a variable into the name of an argument like that.
您不能将变量传递给这样的参数名称。
Instead what you can do is:
相反,你可以做的是:
df <- data.frame(placeholder_name = 1:4)
names(df)[names(df) == "placeholder_name"] <- col.name
or use the default name of "V1"
:
或使用默认名称“V1”:
df <- data.frame(1:4)
names(df)[names(df) == "V1"] <- col.name
or assign by position:
或按职位分配:
df <- data.frame(1:4)
names(df)[1] <- col.name
or if you only have one column just replace the entire names
attribute:
或者,如果您只有一列,则只需替换整个名称属性:
df <- data.frame(1:4)
names(df) <- col.name
There's also the set_names
function in the magrittr
package that you can use to do this last solution in one step:
magrittr包中还有set_names函数,您可以使用它来一步完成最后一个解决方案:
library(magrittr)
df <- set_names(data.frame(1:4), col.name)
But set_names
is just an alias for:
但是set_names只是一个别名:
df <- `names<-`(data.frame(1:4), col.name)
which is part of base R. Figuring out why this expression works and makes sense will be a good exercise.
这是基础R的一部分。弄清楚为什么这个表达有效并且有意义将是一个很好的练习。
#2
3
In addition to ssdecontrol's answer, there is a second option.
除了ssdecontrol的答案,还有第二种选择。
You're looking for mget
. First assign the name to the variable, then the value to the variable that you have previously assigned. After that, mget will evaluate the string and pass it to data.frame.
你在寻找mget。首先将名称分配给变量,然后将值赋给先前分配的变量。之后,mget将评估字符串并将其传递给data.frame。
assign(col.name, "col1")
assign(paste(col.name), 1:4)
df <- data.frame(mget(col.name))
print(df)
col1
1 1
2 2
3 3
4 4
#3
0
I don't recommend you do this, but:
我不建议你这样做,但是:
col.name <- 'col1'
eval(parse(text=paste0('data.frame(', col.name, '=1:4)')))