Message-ID: <CAF8bMcYL+jOx4VA6cZmaU-spFOZ9cJ4UCscT_jv-2rWT_txUcA@mail.gmail.com>
Date: 2016-05-13T17:22:21Z
From: William Dunlap
Subject: apply formula over columns by subset of rows in a dataframe (to get a new dataframe)
In-Reply-To: <2059033841.11937030.1463150695746.JavaMail.zimbra@arpa.veneto.it>
ave() encapsulates the split/lapply/unsplit stuff so
transform(mydf, v1.mod = ave(v1, blocks, FUN=mynorm))
also gives what you got above.
Bill Dunlap
TIBCO Software
wdunlap tibco.com
On Fri, May 13, 2016 at 7:44 AM, Massimo Bressan <
massimo.bressan at arpa.veneto.it> wrote:
> yes, thanks
>
> you pointed me in the right direction: split/unplist was the trick
>
> I completely left behind that possibility!
>
> here the final version
>
> ############
>
> mynorm <- function(x) {(x - min(x, na.rm=TRUE))/(max(x, na.rm=TRUE) -
> min(x, na.rm=TRUE))}
>
> 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))
>
> g <- mydf$blocks
> l <- split(mydf, g)
> l <- lapply(l, transform, v1.mod = mynorm(v1))
> mydf_new <- unsplit(l, g)
>
> ############
>
> thanks again
>
> massimo
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
[[alternative HTML version deleted]]