I am using ddply to aggregate my data but haven’t found an elegant way to assign column names to the output data frame.
At the moment I am doing this:
agg_data <- ddply(raw_data, .(id, date, classification), nrow)
names(agg_data)[4] <- "no_entries"
and this
agg_data <- ddply(agg_data, .(classification, date), colwise(mean, .(no_entries)) )
names(agg_data)[3] <- "avg_no_entries"
Is there a better, more elegant way to do this?
You can use
summarise:or you can use
length(<column_name>)ifnrow(piece)doesn’t work. For instance, here’s an example that should be runnable by anyone:or
EDIT
Or as Joshua comments, the all caps version,
NROWdoes the checking for you.