[R] transforming data based on factors in a dataframe
David Winsemius
dwinsemius at comcast.net
Wed Apr 25 19:17:32 CEST 2012
On Apr 25, 2012, at 10:57 AM, Carly Huitema wrote:
> Hello R-help list,
>
> I would really appreciate help with my factoring problem.
>
> My generated data is this:
>
> df <- expand.grid(T=seq(10,80, by=5), conc=rep(c(1, 3, 7), 2))
> df$curve <- as.factor(rep(1:6, each=length(seq(10,80, by=5))))
> df$counts <- 3*df$T/df$conc + rnorm(df$T,0,2)
>
> plot(counts~T, df)
>
>
> What I would like to do add a new column to the dataframe of zeroed
> data (say df$counts.zeroed). For each curve (designated by factor
> df$curve) I want to take the value in counts and subtract the minimum
> value for that curve.
translated to R that request would be something along these minimally
tested lines:
df$counts.zeroed <- with( df, ave(counts, curve, FUN=function(x){ x-
min(x) }) )
> However, I don't really have an idea of how to
> approach this problem and haven't found anything in my searches.
>
> And just as a second question, my second line of code assigns the
> factors, but if there is a nicer way of doing this I would really
> appreciate knowing how.
There is a `gl` function that I think creates grouping factors, but I
generally use `rep` because I understand it better.
>
> Thanks for any help!
> Carly
>
> ______________________________________________
> R-help at r-project.org mailing list
> 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.
David Winsemius, MD
West Hartford, CT
More information about the R-help
mailing list