[R] apply formula over columns by subset of rows in a dataframe (to get a new dataframe)

Massimo Bressan massimo.bressan at arpa.veneto.it
Fri May 13 13:55:39 CEST 2016


hi 

I need to apply a user defined formula over some selected columns of a dataframe by subsetting group of rows (blocks) and get back a new dataframe 

I’ve been managed to get the the calculations right but I’m not satisfied at all by the form of the results 

please refer to my reproducible example 

########## 
# my user function (an example) 
mynorm <- function(x) {(x - min(x, na.rm=TRUE))/(max(x, na.rm=TRUE) - min(x, na.rm=TRUE))} 

# my dataframe to apply the formula by blocks 
mydf<-data.frame(blocks=rep(c("a","b","c"),each=5), v1=round(runif(15,10,25),0), v2=round(rnorm(15,30,5),0)) 


#my attempts (not satisfied by final output) 

tapply(mydf$v1, mydf$blocks, mynorm) 

byf<-factor(mydf$blocks) 
aggregate(mydf[2:3], list(byf), mynorm) 
aggregate(mydf[2:3], list(mydf$blocks), mynorm, simplify = FALSE) 

########### 

please can anyone give me some hints on how to properly proceed? 

I need a dataframe with all variables as final result 
sorry but I’m sort of definitely stuck with this… 

thanks 


	[[alternative HTML version deleted]]



More information about the R-help mailing list