[R] An averaged model based on Quartile and Mean of Quartiles

David Winsemius dwinsemius at comcast.net
Sat May 21 17:13:57 CEST 2016


> On May 21, 2016, at 7:41 AM, ch.elahe via R-help <r-help at r-project.org> wrote:
> 
> Hi all,
> 
> Here is my df and I want to make an averaged model of my variables based on Time like the following:
> 
> 
>   $ Protocol   : Factor w/ 48 levels "DP FS QTSE SAG",..: 2 3 43 42 
>   $ Time       : num  182 185 189 234 186 ... 
> 
>   $ Systemtype   : Factor w/ 2 levels "Aera XJ","AERA XQ": 1 1 1 
>   $ ADJ        : Factor w/ 2 levels "auto","manu": 1 1 
>   $ BR         : int  384 384 384 384 512 384
>   $ TF         : int  10 10 13 7 7 5 5 
> I split my df into quartiles for time,
> 
> 
>   df$quant=findInterval(df$Time, quantile(df$Time), rightmost.closed=TRUE)
> by the column df$quant I see quartiles 1 to 4 for time, next I want to create a new column for example for BR and if df$quant==1 then put mean of first quartile of BR in that,if df$quant==2 put mean of second quartile of BR in that and so on. I do the same for every numeric variable. Does anyone know how to that?
> 

Perhaps, but untested in the absence of a reproducible example and choosing to instead use a name for the dataframe which is not also a function name:

aggregate( dfrm[ , sapply( dfrm, inherits, "numeric")],   # logical indexing for `[, j]`
           dfrm['quant']      # using "[" keeps it a list
           mean)

-- 

David Winsemius
Alameda, CA, USA



More information about the R-help mailing list