有没有办法将命名变量和包含带空格的变量名的对象都传递给ddply?这是我的意思的一个例子:
> library(plyr)
> dat <- data.frame(id1=rep(c("A","B"),8),id2=rep(c("C","D","E","F"),4),x=rnorm(16))
> vname <- "id2"
#works without spaces
> ddply(dat,c("id1",vname),colwise(sum))
id1 id2 x
1 A C -1.1215505
2 A E -1.4507586
3 B D 1.5064013
4 B F -0.5428153
> vname <- "id 2"
> names(dat)[2] <- vname
#breaks with spaces
> ddply(dat,c("id1",vname),colwise(sum))
Error in parse(text = x) : <text>:1:4: unexpected numeric constant
1: id 2
^
#backticks work when manually entering the name
> ddply(dat,c("id1","`id 2`"),colwise(sum))
id1 id 2 x
1 A C -1.1215505
2 A E -1.4507586
3 B D 1.5064013
4 B F -0.5428153
#backticks don't work when pointing to object containing name
> ddply(dat,c("id1",`vname`),colwise(sum))
Error in parse(text = x) : <text>:1:4: unexpected numeric constant
1: id 2
^