rrs
rrs

Reputation: 9913

How to use aggregate when column names are numbers?

I'm having problems getting aggregate to work on a data frame that has variable names which are "numbers". Here's an example:

library(stringr)

# this works
df <- data.frame(x001=runif(10),x002=runif(10),category=rep(1:2,5))
aggregate(. ~ category, data=df, mean)

# this doesn't
names(df) <- str_replace(names(df),"x","")
aggregate(. ~ category, data=df, mean)

What am I doing wrong?

Upvotes: 1

Views: 153

Answers (1)

Matthew Plourde
Matthew Plourde

Reputation: 44614

I agree with @orizon's point about using non-standard names, but to answer your question, you would tick the names.

aggregate(cbind(`001`, `002`) ~ category, data=df, mean)

#   category     `001`     `002`
# 1        1 0.6600887 0.3927709
# 2        2 0.5115426 0.5189629

EDIT:

This would be better, and more general:

agg <- aggregate(. ~ category, data=setNames(df, make.names(names(df))), mean)
#   category      X001      X002
# 1        1 0.7426327 0.4081779
# 2        2 0.6666881 0.5903070

and if you really wanted bad names in your aggregation, too:

names(agg) <- gsub('^X', '', names(agg))

Upvotes: 2

Related Questions