Hi I'm hoping someone can help me I am a relative newbie to R. I have data that is in a similar format to this... Experiment Score1 Score2 X -0.85 -0.02 X -1.21 -0.02 X 1.05 0.09 Y -1.12 -0.07 Y -0.27 -0.07 Y -0.93 -0.08 Z 1.1 -0.03 Z 2.4 0.09 Z -1.0 0.09 Now I can easily have a look at the overall correlation of score 1 and 2 by doing this plot(data[,2], data[,3]) or fit <- lm(data[,2] ~ data[,3] BUT! I really want to look at the correlations within each experiment type so ideally a multiple plot per page of each correlation within an experiment - and/or a way of looping through the data to get the simple linear regression for each experiment for scores 1 and 2. This looks like an easy example but I have thousands of results so it would be really hand to find away of doing this quickly!! Let me know if you need more explaining... E -- View this message in context: http://n4.nabble.com/Mutliple-sets-of-data-in-one-dataset-Need-a-loop-tp1018503p1018503.html Sent from the R help mailing list archive at Nabble.com.
David Winsemius
2010-Jan-20 17:29 UTC
[R] Mutliple sets of data in one dataset....Need a loop?
On Jan 20, 2010, at 11:07 AM, BioStudent wrote:> > Hi > > I'm hoping someone can help me I am a relative newbie to R. > > I have data that is in a similar format to this... > > Experiment Score1 Score2 > X -0.85 -0.02 > X -1.21 -0.02 > X 1.05 0.09 > Y -1.12 -0.07 > Y -0.27 -0.07 > Y -0.93 -0.08 > Z 1.1 -0.03 > Z 2.4 0.09 > Z -1.0 0.09 > > Now I can easily have a look at the overall correlation of score 1 > and 2 by > doing this > plot(data[,2], data[,3]) or > fit <- lm(data[,2] ~ data[,3] > > BUT! I really want to look at the correlations within each > experiment type > so ideally a multiple plot per page of each correlation within an > experiment > - and/or a way of looping through the data to get the simple linear > regression for each experiment for scores 1 and 2. This looks like > an easy > example but I have thousands of results so it would be really hand > to find > away of doing this quickly!!library(lattice) ?xyplot # a group specification ?panel.lmline There are quite a few examples at the xyplot help page.>-- David Winsemius, MD Heritage Laboratories West Hartford, CT
David Freedman
2010-Jan-20 18:08 UTC
[R] Mutliple sets of data in one dataset....Need a loop?
You'll probably want to look at the 'by' function d=data.frame(sex=rep(1:2,50),x=rnorm(100)) d$y=d$x+rnorm(100) head(d) cor(d) by(d[,-1],d['sex'],function(df)cor(df)) You might also want to look at the doBy package -- View this message in context: http://n4.nabble.com/Mutliple-sets-of-data-in-one-dataset-Need-a-loop-tp1018503p1018616.html Sent from the R help mailing list archive at Nabble.com.
Glen Sargeant
2010-Jan-20 20:18 UTC
[R] Mutliple sets of data in one dataset....Need a loop?
One way to plot subsets of data identified by a grouping variable is to use lapply() on a list of subsets. The approach is worth mentioning because similar tactics are useful for many problems. #List of unique values for grouping variable #that is not necessarily a factor names <- as.list(unique(df$Experiment)) #List of dataframes; 1 for each unique value of grouping variable df.lst <- lapply(names,function(name)subset(df,Experiment==name)) #Name components of the list #Not necessary in this case... but permits indexing by level #of the grouping variable names(df.lst) <- names #Now you can use lapply() to carry out the same operation on #each component of your list. For example, to send plots to #a pdf with 1 page for each component: pdf("plot.pdf") lapply(df.lst,function(df)plot(df[,2],df[,3])) dev.off() ----- Glen Sargeant Research Wildlife Biologist -- View this message in context: http://n4.nabble.com/Mutliple-sets-of-data-in-one-dataset-Need-a-loop-tp1018503p1018714.html Sent from the R help mailing list archive at Nabble.com.
Maybe Matching Threads
- All combinations possible in a mutliple regression
- Multiple Conditional Tranformations
- specifying an unbalanced mixed-effects model for anova
- Adding Legend about two quantile lines at ggplot
- Help with replacement of certain values in dataset with SAS code equivalent