使用变量值作为data.frame或cbind中的列名 [英] Using variable value as column name in data.frame or cbind
本文介绍了使用变量值作为data.frame或cbind中的列名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在创建数据框时(或在类似情况下,例如使用cbind),R中是否有一种方法可以将变量评估为列名?
Is there a way in R to have a variable evaluated as a column name when creating a data frame (or in similar situations like using cbind)?
例如
a <- "mycol";
d <- data.frame(a=1:10)
这将创建一个数据框,其中包含一个名为a
而不是mycol
的列.
this creates a data frame with one column named a
rather than mycol
.
这比帮助我从代码中删除很多行的情况不重要:
This is less important than the case that would help me remove quite a few lines from my code:
a <- "mycol";
d <- cbind(some.dataframe, a=some.sequence)
我当前的代码受了折磨:
My current code has the tortured:
names(d)[dim(d)[2]] <- a;
从美学上讲是荒唐的.
推荐答案
> d <- setNames( data.frame(a=1:10), a)
> d
mycol
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
10 10
这篇关于使用变量值作为data.frame或cbind中的列名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文