[R] using "aggregate" when variable names contain spaces

Dimitri Liakhovitski dimitri.liakhovitski at gmail.com
Mon Apr 18 21:19:44 CEST 2011


Hello!

my data set has many variables. Unfortuantely, many of those variables
contain spaces in their names.
I need advice on: how to refer to variable names in the formula for
"aggregate". See example below:

### Generating example data set:
mydate = rep(seq(as.Date("2008-12-01"), length = 3, by = "month"),4)
value1=c(1,10,100,2,20,200,3,30,300,4,40,400)
value2=c(1.1,10.1,100.1,2.1,20.1,200.1,3.1,30.1,300.1,4.1,40.1,400.1)
example<-data.frame(mydate=mydate,value1=value1,value2=value2)
example$group<-c(rep("group1",3),rep("group2",3),rep("group1",3),rep("group2",3))
exampe$group<-as.factor(exampe$group)

### Generating variable names with spaces:
names(example)<-c("mydate", "my value 1","my value 2","group")

### Trying to aggregate - but it's not working. Clearly, my reference
to variable names is incorrect:
mynames<-names(example)
example.agg1<-aggregate(cbind(mynames)~group+mydate,sum,data=example)


Thank you very much!

-- 
Dimitri Liakhovitski
Ninah Consulting
www.ninah.com



More information about the R-help mailing list