Ddply and spaces in quoted variables

Can spaces be used in ddply?

I am using data from a spreadsheet with a lot of spaces in the column names, and I would like to keep these names because later 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 will lose the original column names.

However ddply doesn't look 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
+5
source share
2 answers

Wrapping column names in single reverse ticks (`) seems to do the trick.

ddply(df, "`fac tor`", summarize, r.avg=mean(response))

, , .

ddply(df, 2, summarize, r.avg=mean(response))
+8

- , :

lev=gl(2, 3, labels=c("low", "high"))
df=data.frame(factor=lev, "fac tor"=lev, response=1:6, check.names = FALSE)
colnames(df) <- gsub(" ","~",colnames(df))
+1

All Articles