there already was a similar question which answer helped somewhat, but I couldn't translate it to my use case when using aggregate within lapply. With setNames I can specify a string, but I'm having a hard time pulling out the column name lapply is currently working on to use in setNames.
So, I have a df.
head(rms)
file date min fullband band1 band2 band3 band4 band5 hr
1 0 2015/1/14 0 112.17 112.43 94.13 97.92 102.17 96.87 0
2 1 2015/1/14 5 111.73 110.71 94.01 96.78 102.20 96.90 0
3 2 2015/1/14 10 109.08 107.05 91.81 96.68 102.40 97.01 0
4 3 2015/1/14 15 110.74 109.24 93.14 96.65 102.02 96.87 0
5 4 2015/1/14 20 108.82 107.09 93.16 96.50 102.08 96.84 0
And I aggregate the columns fullband-band5 like this:
rms.byhr<-lapply(rms[-c(1:3,10)], function(x){
aggregate(x, by=list(rms$hr), mean)
})
However, naturally, lapply will use the column name for the list elements and replace the names of the df it creates with something arbitrary (Group.1 and x).
I tried:
rms.byhr<-lapply(rms[-c(1:3,10)], function(x){
setNames(aggregate(x, by=list(rms$hr), mean), c("Hour", names(x))
})
and
rms.byhr<-lapply(rms[-c(1:3,10)], function(x){
setNames(aggregate(x, by=list(rms$hr), mean), c("Hour", names(rms)[which(names(rms)==names(x))]))
})
But that doesn't seem to work and returns NA. So I guess my question really is, how does "x" look like in lapply and how to I index/pull out the name properly?
I need them named for subsequent functions.