[R] matrix calculation
David Winsemius
dwinsemius at comcast.net
Sat Dec 10 15:54:07 CET 2011
On Dec 10, 2011, at 9:13 AM, R. Michael Weylandt wrote:
> Perhaps something like this (untested) -- it's going to depend on the
> exact structure of your data so if this doesn't work, please use
> dput() to send a plain text representation:
>
> tapply(data, data$animal, function(d) d[, c("A01", "A02")] - d[d$time
> == "d0", c("A01", "A02")] )
`tapply` is not documented to work on dataframes as a first argument.
I think you may want to use aggregate or the split/lapply approach.
--
David.
>
> In short, take "data" split it into groups by "data$animal" and then
> apply the function which consists of subtracting the "A01" and "A02"
> columns by the row that has time == "d0"
>
> Michael
>
> On Fri, Dec 9, 2011 at 1:44 PM, Junyu Lee <junyu0813 at gmail.com> wrote:
>> Hello,
>>
>> I have a matrix
>>
>> animal time A01 A02
>> A d0 -5.4 2.7
>> A d112 4.6 5.9
>> A d224 3.9 6.3
>> B d0 7.1 5.6
>> B d112 1.5 3.2
>> B d224 2.9 3.6
>> C d112 3.7 5.8
>> C d0 7.1 NA
>> C d224 4.2 5.7
>>
>> I have three animal A, B, C. Each animal have two measurements(A01
>> and A02)
>> at three different time points (d0, d112 and d224). I'd like to
>> calculate:
>>
>> animal A:
>>
>> A01 at d112 (4.6) - A01 at d0 (-5.4)
>> A01 at d224 (3.9) - A01 at d0 (-5.4)
>>
>> A02 at d112 (5.9) - A02 at d0 (2.7)
>> A02 at d224 (6.3) - A02 at d0 (2.7)
>>
>> Same for animal B and C
>>
>> I really appreciate your help.
>>
>> Junyu
>>
>> [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> 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.
>
> ______________________________________________
> 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