keith.w.lar...@gmail.com wrote on 12/27/2011 04:06:09 PM:

> Jean,
> 
> Thanks! I am new to the list and R so I am unsure of the protocol for
> replying to a reply. 


In general, cc R-help so that the entire thread of communication is 
available to everyone


> Your code is simple and elegant. I understand
> most of it, but I do not understand what is happening in the 'sapply'
> portion of your code. 


read
?sapply

lapply(X) returns a list of the same length as X, each element of which is 
the result of applying FUN to the corresponding element of X.

sapply(X) is a user-friendly version and wrapper of lapply(X) by default 
returning a vector, matrix ...


> Second, I know I can create a dataframe from the
> results by:
> 
> wing.pearson <- t(sapply(wing.list, function(temp)
>                                     cor.test(temp$Delta13C,
> temp$FeatherPosition,
>                                     method="pearson")[c("estimate",
> "p.value")]))
> 
> but when I do this I get the Individual_ID's as row labels, not as a
> variable 'Individual_ID' in my new data frame. Any suggestions?


Actually, the results are in a matrix.  Use the data.frame() function to 
convert the matrix to a data frame.  Then add a new column with the IDs if 
you like.

wing.list <- split(WW_Wing_SI, WW_Wing_SI$Individual_ID)
wing.pearson <- data.frame(t(sapply(wing.list, function(temp) 
cor.test(temp$Delta13C,
        temp$FeatherPosition, method="pearson")[c("estimate", 
"p.value")])))
wing.pearson$Individual_ID <- dimnames(wing.pearson)[[1]]
wing.pearson


> Many Thanks!


You're welcome.

Jean


> Keith
> 
> 
> On Tue, Dec 27, 2011 at 10:41 PM, Jean V Adams <jvad...@usgs.gov> wrote:
> >
> > Keith Larson wrote on 12/27/2011 03:05:48 PM:
> >
> >
> >> Dear all,
> >>
> >> I would like to conduct a Pearson's correlation using cor.test
> >> separately for each individual in my data set. Each individual has
> >> nine observations of the measurement variable 'Delta13C'. I can 
figure
> >> our how to do it manually one 'Individual_ID' at a time, but I cannot
> >> figure out how to automate the process (a loop I am guessing). In
> >> addition, I would like to generate a new data frame that has the each
> >> individual's 'Individual_ID', the pearson correlation 'estimate', and
> >> the 'p.valve'.
> >>
> >> The output table would look something like:
> >>
> >> Individual_ID, estimate, p.value
> >> WW_08A_02, -0.7670675, 0.01586
> >> WW_08A_03, -0.02320767, 0.9527
> >>
> >> Code with sample data:
> >>
> >> ## Create sample dataset
> >> WW_Wing_SI <-
> >> structure(list(Individual_ID = c("WW_08A_02", "WW_08A_02", 
"WW_08A_02",
> >> "WW_08A_02", "WW_08A_02", "WW_08A_02", "WW_08A_02", "WW_08A_02",
> >> "WW_08A_02", "WW_08A_03", "WW_08A_03", "WW_08A_03", "WW_08A_03",
> >> "WW_08A_03", "WW_08A_03", "WW_08A_03", "WW_08A_03", "WW_08A_03"
> >> ), FeatherPosition = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4,
> >> 5, 6, 7, 8, 9), Delta13C = c(-18.67, -19.16, -20.38, -20.96,
> >> -21.61, -21.65, -21.31, -20.8, -21.28, -20.06, -20.3, -21.21,
> >> -22.9, -22.87, -21.13, -20.68, -20.58, -20.69)), .Names =
> >> c("Individual_ID",
> >> "FeatherPosition", "Delta13C"), row.names = c(NA, 18L), class =
> >> "data.frame")
> >>
> >>
> >> ## Create vector with unique individual IDs
> >> WW_Wing_Individuals <- unique(WW_Wing_SI$Individual_ID)
> >>
> >> ## Create temp dataset with for the first individual
> >> temp <- WW_Wing_SI_Spring[ which(WW_Wing_SI_Spring$Individual_ID ==
> >> WW_Wing_Individuals[1]), ]
> >>
> >> ## Create temp2 dateset with results of pearsons product-moment
> >> correlation (for the first individual)
> >> temp2 <- cor.test(temp$Delta13C, temp$FeatherPosition, 
method="pearson")
> >>
> >> Many Cheers,
> >> Keith
> >>
> >>
> >> 
> >> Keith Larson, PhD Student
> >> Evolutionary Ecology, Lund University
> >> Sölvegatan 37
> >> 223 62 Lund Sweden
> >> Phone: +46 (0)46 2229014 Mobile: +46 (0)73 0465016 Fax: +46 (0)46 
2224716
> >> Skype: sternacaspia FB: keith.w.lar...@gmail.com
> >
> >
> > # split your data frame according the the individual IDs
> > wing.list <- split(WW_Wing_SI, WW_Wing_SI$Individual_ID)
> >
> > # then apply cor.test() with extract to each element of the list
> > t(sapply(wing.list, function(temp)
> >         cor.test(temp$Delta13C, temp$FeatherPosition,
> >         method="pearson")[c("estimate", "p.value")]))
> >
> > Jean

        [[alternative HTML version deleted]]

______________________________________________
R-help@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.

Reply via email to