ddply和带引号的变量中的空格 [英] ddply and spaces in quoted variables
问题描述
是否可以在ddply中使用空格?
Is it possible to use spaces in ddply?
我正在使用来自电子表格的数据,其中列名中有很多空格,我想保留这些名称,因为稍后我要使用与原始列名相同的列名导出该数据.一共有200多个列,使用make.names当然会给我合适的名字,但后来我失去了原来的列名.
I'm using data from a spreadsheet with a lot of spaces in column names and i would like to keep those names because later on I want to export this data with the same column names as the original. There are 200+ columns and using make.names will of course give me proper names but then I lose the original column names.
但是ddply似乎不喜欢空格吗?有解决方法吗?
However ddply doesn't seem to like spaces? Is there a workaround?
lev=gl(2, 3, labels=c("low", "high"))
df=data.frame(factor=lev, "fac tor"=lev, response=1:6, check.names = FALSE)
> ddply(df, c("factor"), summarize, r.avg=mean(response))
factor r.avg
1 low 2
2 high 5
> ddply(df, c("fac tor"), summarize, r.avg=mean(response))
Error in parse(text = x) : <text>:1:5: unexpected symbol
: fac tor
推荐答案
用单反勾号(`)包裹列名似乎可以解决问题.
Wrapping the column names in single back ticks (`) seems to do the trick.
ddply(df, "`fac tor`", summarize, r.avg=mean(response))
您还可以使用可能不吸引人的列索引,具体取决于您的data.frame的大小以及您事先对各列位置的了解.
You can also use column indices which may or may not be appealing depending on how big your data.frame is and your knowledge of the locations of each column beforehand.
ddply(df, 2, summarize, r.avg=mean(response))
这篇关于ddply和带引号的变量中的空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!