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: check names? #27

Closed MatthieuStigler closed 10 years ago

MatthieuStigler commented 12 years ago

Hi

When the variable that is getting casted into a new column has white spaces, columns of the new data frame with contain the same names, with the white spaces too.

aqm <- melt(airquality, id=c("Month", "Day"), na.rm=TRUE) aqm$variable2<-factor(aqm$variable, levels=levels(aqm$variable), labels=gsub("Ozone", "ozone with space", levels(aqm$variable))) df<-dcast(aqm, day ~ variable2) colnames(df)

This is in itself not a big problem, but makes some ways of indexing columns no more possible, or adds difficulties to handle with ggplot. The solution is very simple, involving only: colnames(df) <- check.names(colnames(df))

it would be nice however if this could be directly made an argument of dcast() & co, making the code more compact and elegant!

Thanks!!

(this was discussed on manipulatr: http://groups.google.com/group/manipulatr/browse_thread/thread/60579a3cb89253b/b5738362f7c08a97?lnk=gst&q=stigler#b5738362f7c08a97)

hadley commented 10 years ago

I now prefer to leave variables as is, since you can access with backquotes.