Keith Larson
2011-Dec-28 10:02 UTC
[R] convert variable types when creating data frame from cor.test results
Dear list, The below dataset and code creates a new dataset with the results from the function cor.test being performed on each individual ('Individual_ID') from my original dataset. How do I convert each variable from the cor.test results to a numeric data type, as it is passed into the new dataframe? For example, 'estimate', 'p.value', and 'conf.int' should be numeric not character. Second, I would like variable 'conf.int' to be two variables, 'lowlim' and 'uplim'. Many Cheers, Keith ## 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") # split data frame according the the individual IDs wing.list <- split(WW_Wing_SI, WW_Wing_SI$Individual_ID) # apply cor.test() with extract to each element of the list test <- as.data.frame(t(sapply(wing.list, function(temp) cor.test(temp$Delta13C, temp$FeatherPosition, method="pearson")[c("estimate", "p.value", "conf.int")]))) ******************************************************************************************* 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.larson at gmail.com
Petr PIKAL
2011-Dec-28 15:51 UTC
[R] convert variable types when creating data frame from cor.test results
Hi> > Dear list, > > The below dataset and code creates a new dataset with the results from > the function cor.test being performed on each individual > ('Individual_ID') from my original dataset. How do I convert each > variable from the cor.test results to a numeric data type, as it is > passed into the new dataframe? For example, 'estimate', 'p.value', and > 'conf.int' should be numeric not character. Second, I would like > variable 'conf.int' to be two variables, 'lowlim' and 'uplim'. > > Many Cheers, > Keith > > ## 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")> > # split data frame according the the individual IDs > wing.list <- split(WW_Wing_SI, WW_Wing_SI$Individual_ID) > > # apply cor.test() with extract to each element of the list > test <- as.data.frame(t(sapply(wing.list, function(temp) > cor.test(temp$Delta13C,temp$FeatherPosition,> method="pearson")[c("estimate", > "p.value", "conf.int")])))The problem is that transposing a sapply object transforms it to matrix and matrix can have only one type of values. Not very sophisticated but this result <- unlist(sapply(wing.list, function(temp) cor.test(temp$Delta13C, temp$FeatherPosition, method="pearson"))[c(3,4,9),]) gives you a numeric vector of desired values. You can change it to numeric matrix dim(result)<-c(4,2) Give the propper name to rows and columns and/or transpose according to your wish. Regards Petr> >*******************************************************************************************> 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)462224716> Skype: sternacaspia FB: keith.w.larson at gmail.com > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.
Jean V Adams
2011-Dec-28 16:28 UTC
[R] convert variable types when creating data frame from cor.test results
Keith Larson wrote on 12/28/2011 04:02:39 AM:> Dear list, > > The below dataset and code creates a new dataset with the results from > the function cor.test being performed on each individual > ('Individual_ID') from my original dataset. How do I convert each > variable from the cor.test results to a numeric data type, as it is > passed into the new dataframe? For example, 'estimate', 'p.value', and > 'conf.int' should be numeric not character. Second, I would like > variable 'conf.int' to be two variables, 'lowlim' and 'uplim'. > > Many Cheers, > Keith > > ## 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")> > # split data frame according the the individual IDs > wing.list <- split(WW_Wing_SI, WW_Wing_SI$Individual_ID) > > # apply cor.test() with extract to each element of the list > test <- as.data.frame(t(sapply(wing.list, function(temp) > cor.test(temp$Delta13C,temp$FeatherPosition,> method="pearson")[c("estimate", > "p.value", "conf.int")]))) > >*******************************************************************************************> 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)462224716> Skype: sternacaspia FB: keith.w.larson@gmail.comIf you unlist() the call to cor.test, you will end up with numeric data. You will also end up with two variables for the confidence intervals. test <- data.frame(t(sapply(wing.list, function(temp) unlist(cor.test(temp$Delta13C, temp$FeatherPosition, method="pearson")[c("estimate", "p.value", "conf.int")])))) test estimate.cor p.value conf.int1 conf.int2 WW_08A_02 -0.76706752 0.01585509 -0.9481677 -0.2098478 WW_08A_03 -0.02320767 0.95274294 -0.6768966 0.6509469 Jean [[alternative HTML version deleted]]
Maybe Matching Threads
- How to create a loop and then extract values from the list generated by cor.test
- calculating correlation coefficients on repeated measures
- Identifying records with the correct number of repeated measures
- Subsetting without partial matches
- doing zero inflated glmm for count data with fmr