从 data.frame 命令中的列表指定列名称
我有一个名为 cols
的列表,其中包含列名称:
cols <- c('Column1','Column2','Column3')
我想重现这个命令,但调用列表:
data.frame(Column1=rnorm(10))
以下是我尝试时发生的情况:
>; data.frame(cols[1]=rnorm(10))
错误:“data.frame(I(cols[1])=”中出现意外的“=”
同样的事情发生如果我将 cols[1]
包装在 I()
或 eval()
中,
如何将该项目从向量输入到 中。 >data.frame()
命令?
更新:
对于一些背景,我定义了一个函数calc.means()
,它接受一个数据框和一个列表变量并执行大型且复杂的 ddply 操作,在变量指定的级别进行总结。
我尝试使用 data.frame() 命令执行的操作是向上返回聚合级别。在最顶部,在每一步重新运行 calc.means() 并使用 rbind() 将结果相互粘合,我需要添加带有 ' 的虚拟列。为了让 rbind 正常工作,
我基本上将类似 cast
的边距功能滚动到 ddply 中,并且我不想每次运行都重新输入列名称。这是完整的代码:
cols <- c('Col1','Col2','Col3')
rbind ( calc.means(dat,cols),
data.frame(cols[1]='All', calc.means(dat, cols[2:3])),
data.frame(cols[1]='All', cols[2]='All', calc.means(dat, cols[3]))
)
I have a list called cols
with column names in it:
cols <- c('Column1','Column2','Column3')
I'd like to reproduce this command, but with a call to the list:
data.frame(Column1=rnorm(10))
Here's what happens when I try it:
> data.frame(cols[1]=rnorm(10))
Error: unexpected '=' in "data.frame(I(cols[1])="
The same thing happens if I wrap cols[1]
in I()
or eval()
.
How can I feed that item from the vector into the data.frame()
command?
Update:
For some background, I have defined a function calc.means()
that takes a data frame and a list of variables and performs a large and complicated ddply operation, summarizing at the level specified by the variables.
What I'm trying to do with the data.frame()
command is walk back up the aggregation levels to the very top, re-running calc.means()
at each step and using rbind()
to glue the results onto one another. I need to add dummy columns with 'All' values in order to get the rbind to work properly.
I'm rolling cast
-like margin functionality into ddply, basically, and I'd like to not retype the column names for each run. Here's the full code:
cols <- c('Col1','Col2','Col3')
rbind ( calc.means(dat,cols),
data.frame(cols[1]='All', calc.means(dat, cols[2:3])),
data.frame(cols[1]='All', cols[2]='All', calc.means(dat, cols[3]))
)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用可以使用
结构
:但我不明白你为什么这样做!
Use can use
structure
:I don't get why you are doing this though!
我不确定如何直接执行此操作,但您可以简单地跳过在 data.frame() 命令中分配名称的步骤。假设您将 data.frame() 的结果存储在名为 foo 的变量中,您可以
简单地执行以下操作:
names(foo) <- cols
在创建数据框后
I'm not sure how to do it directly, but you could simply skip the step of assigning names in the data.frame() command. Assuming you store the result of data.frame() in a variable named foo, you can simply do:
names(foo) <- cols
after the data frame is created
有一个技巧。你可能会弄乱列表:
然后,如果你使用 one paren 来调用列表,那么你应该得到你想要的东西
你可以将它即时用作
setNames(list("All") ), cols[1])
但我认为它不太优雅。例子:
There is one trick. You could mess with lists:
Then if you use call to list with one paren then you should get what you want
You could use it on-the-fly as
setNames(list("All"), cols[1])
but I think it's less elegant.Example:
我相信 allocate() 函数就是您的答案:
返回:
使用 lapply() 或 sapply() 函数,您应该能够循环 cbind() 过程。类似于:
返回:
然后,清理列名称:
返回:
干杯,
亚当
I believe the assign() function is your answer:
Returns:
With the lapply() or sapply() function, you should be able to loop the cbind() process. Something like:
Returns:
Then, to clean up the column names:
Returns:
Cheers,
Adam