[R] linear regression in a ragged array

David Winsemius dwinsemius at comcast.net
Mon Mar 21 12:52:44 CET 2011


On Mar 21, 2011, at 5:25 AM, Marcel Curlin wrote:

> Hello,
> I have a large dataset of the form
>
> subj   var1   var2
> 001    100    200
> 001    120    226
> 001    130    238
> 001    140    245
> 001    150    300
> 002    110    205
> 002    125    209
> 003    101    233
> 003    115    254
>
> I would like to perform linear regression of var2 on var1 for each  
> subject
> separately. It seems like I should be able to use the tapply  
> function as you
> do for simple operations (like finding a mean of var1 for each  
> subject), but
> I am not sure of the correct syntax for this. Is there a way to do  
> this?
>

tapply works on vectors, split works on data.frames.

lapply(split(dat, dat$subj), function(x) lm(var2 ~ var1, data=x))

-- 

David Winsemius, MD
Heritage Laboratories
West Hartford, CT



More information about the R-help mailing list