Allow make.names() in dcast for proper column names?

42 views
Skip to first unread message

Matthieu

unread,
Jun 12, 2012, 6:05:06 AM6/12/12
to manipulatr
Hi

I have issues when the variable that is getting casted into a new
column has white spaces for example, as columns get names with white
spaces, which cause then many problems. Typically:

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)

Is there an easy solution for this? I wonder whether it would be
possible to have an optional argument "make.names=FALSE" in dcast, so
that values that get transformed into column names have a
syntactically correct name (using make.names() to correct) ?

Thanks!!

Matthieu

Hadley Wickham

unread,
Jun 16, 2012, 9:56:23 AM6/16/12
to Matthieu, manipulatr
Hi Matthieu,

You can do df$`ozone with space` - otherwise I think I was thinking it
was just as easy to fix the names yourself.

Hadley
> --
> You received this message because you are subscribed to the Google Groups "manipulatr" group.
> To post to this group, send email to manip...@googlegroups.com.
> To unsubscribe from this group, send email to manipulatr+...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/manipulatr?hl=en.
>



--
Assistant Professor / Dobelman Family Junior Chair
Department of Statistics / Rice University
http://had.co.nz/

Matthieu Stigler

unread,
Jun 22, 2012, 3:09:53 AM6/22/12
to Hadley Wickham, manipulatr
Hi Hadley

Sure, one can still use variables in data frame with spaces in colnames,
although this is not the standard behaviour expected in ggplot for
example. An sure, this is easy to fix, just need to add a single code line
colnames(df) <-make.names(colnames(df))

but because functions are written for lazy people, why not simplify two
lines into one? :-)

Thanks

Mat


Le 16/06/2012 15:56, Hadley Wickham a �crit :

Hadley Wickham

unread,
Jun 25, 2012, 9:13:34 AM6/25/12
to Matthieu Stigler, manipulatr
Hmmmm, I'll think about it. Could you please file an issue at
https://github.com/hadley/plyr/issues so I don't forget about it?

Thanks,

Hadley
Reply all
Reply to author
Forward
0 new messages