[R] transforming data based on factors in a dataframe
jim holtman
jholtman at gmail.com
Wed Apr 25 19:01:42 CEST 2012
try this: (uses 'ave')
> 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)
> df$zero <- ave(df$counts, df$curve, FUN = function(x) x - min(x))
>
> df
T conc curve counts zero
1 10 1 1 30.4148210 0.000000
2 15 1 1 43.2169594 12.802138
3 20 1 1 64.3876491 33.972828
4 25 1 1 75.2957249 44.880904
5 30 1 1 95.4479888 65.033168
6 35 1 1 103.5814348 73.166614
7 40 1 1 121.0131061 90.598285
8 45 1 1 135.8270827 105.412262
9 50 1 1 152.7179565 122.303136
10 55 1 1 165.2928662 134.878045
11 60 1 1 181.1899238 150.775103
12 65 1 1 197.8600462 167.445225
13 70 1 1 210.0112723 179.596451
14 75 1 1 224.1807064 193.765885
15 80 1 1 237.5717249 207.156904
16 10 3 2 8.2411801 0.000000
17 15 3 2 14.7415660 6.500386
18 20 3 2 20.9944483 12.753268
19 25 3 2 23.4369433 15.195763
On Wed, Apr 25, 2012 at 10:57 AM, Carly Huitema <carly.huitema at gmail.com> 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. 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.
>
> 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.
--
Jim Holtman
Data Munger Guru
What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.
More information about the R-help
mailing list