[R] simple loop(?) analysing subsets
Joshua Wiley
jwiley.psych at gmail.com
Mon Jul 19 00:48:35 CEST 2010
Hello,
Take a look at ?by
Something like:
by(data, data[ , "species id"], function(x) {lm(o2con ~ bm, data = x)})
As an aside, it can be a bit cumbersome to deal with spaces in a
variables name (because it has to be quoted then). Also since data is
a function, something like mydata might make it easier to keep code
straight.
HTH,
Josh
On Sun, Jul 18, 2010 at 2:29 PM, karmakiller <roisinmoriarty at gmail.com> wrote:
>
> Hi All,
>
> I have a large data set with many columns of data. One of these columns is a
> species identifier and the remainder are variables such as temperature or
> mass. Currently I am carrying out a single regression on subsets of the data
> set, e.g. separated data sets with only the data from one species at a time.
> I have been searching for a thread that will help me to understand how best
> to repeat this process for each different species identifier in that
> variable column. I can’t seem to find one that is similar to what I am
> trying to do. It might be the case that I am not looking for the right thing
> or that I do not fully understand the process.
>
> How do I run a simple loop that produces a regression for each species as
> identified in the variable species id, which is one column in the large data
> set that I am using?
>
> Simple regression that I wish to repeat
>
> data<- read.table("…/STUDY.txt",header=T)
> names(data)
> model<- with(data,{lm(o2con~bm)})
> summary(model)
>
>
> sample data set
>
> species id o2con bm
> 1 0.5 5
> 1 0.6 2
> 1 0.4 4
> 1 0.4 2
> 1 0.5 3
> 2 0.3 7
> 2 0.4 8
> 2 0.5 3
> 2 0.7 4
> 2 0.9 2
> 3 0.3 6
> 3 0.7 2
> 3 0.4 1
> 3 0.3 7
> 5 0.3 2
> 5 0.6 1
> 5 0.9 7
> 5 0.2 5
>
> I would be very grateful for some help with this. I really like using R and
> I can usually figure out what I want to do but I have been trying to figure
> this out for a while now and I am getting nowhere.
>
> Thank you.
>
> --
> View this message in context: http://r.789695.n4.nabble.com/simple-loop-analysing-subsets-tp2293383p2293383.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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.
>
--
Joshua Wiley
Ph.D. Student, Health Psychology
University of California, Los Angeles
http://www.joshuawiley.com/
More information about the R-help
mailing list