我可以使用变量作为参数名称吗?

时间:2014-03-14 18:01:20

标签: r

说我把我的猫放在这样的矩阵中:

> cats <- cbind(fluffy=c(weight=1,length=2), muffin=c(3,4))

这给了我:

       fluffy muffin
weight      1      3
length      2      4

有没有办法可以使用变量而不是手动指定名称?

这不起作用:

> name <- "whiskers"
> cbind(cats, name=c(5, 6))

       fluffy muffin name
weight      1      3    5
length      2      4    6

这也不是:

> cbind(cats, `name`=c(5, 6))

       fluffy muffin name
weight      1      3    5
length      2      4    6

这也不是:

> cbind(cats, $name=c(5, 6))

Error: unexpected '$' in "cbind(cats, $"

我唯一的选择是做一些讨厌的do.call吗?

> args <- list(cats, c(5,6))
> names(args)[2] <- name
> do.call(cbind, args)

       fluffy muffin whiskers
weight      1      3        5
length      2      4        6

0 个答案:

没有答案
相关问题