ddply and spaces in quoted variables

北战南征 提交于 2019-12-19 06:57:53

问题


Is it possible to use spaces in ddply?

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.

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

回答1:


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

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

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))



回答2:


I would just use a regular expression to convert the spaces to some nonsense character, then convert back at the end:

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))


来源:https://stackoverflow.com/questions/6569079/ddply-and-spaces-in-quoted-variables

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!