hadley / reshape

An R package to flexible rearrange, reshape and aggregate data
http://had.co.nz/reshape
Other
210 stars 56 forks source link

dcast formula fails to resolve ... when column names have spaces #67

Open saurfang opened 9 years ago

saurfang commented 9 years ago
library(reshape2)
aqm <- melt(airquality, id=c("month", "day"), na.rm=TRUE)
colnames(aqm) <- c("my month" , "my day", "variable", "value")
dcast(aqm, ... ~ variable, value.var = "value")

Throws exception

Error in parse(text = x) : <text>:1:4: unexpected symbol
1: my month
       ^

This becomes a problem when I read data using readr which preserves the column names as-is (I like your not messing with my column names by the way). Now if I want to do any dcast using ... shorthand, it would fail. I can work around by constructing the formula manually/programmatically and quote the symbol properly.

philpav commented 5 years ago

The problem is still present :(

MHBailey commented 4 years ago

It's not pretty, but here is a work-around:

newdf <- dcast(aqm, aqm$'my month' + aqm$'my day' ~ variable, value.var = "value")
colnames(newdf)[1] <- "my month"
colnames(newdf)[2] <- "my day"