简体   繁体   中英

how to extract names from lists in R

I have a data frame in R with 2 variables [eg height, weight] I'd like to calculate correlation on along with a few other covariates [such as sex, ethnicity].

I'd like to calculate the R^2 value for height vs. weight for each grouping of sex and ethnicity. Eg R^2 for height vs. weight for Male & Caucasians, Female & Caucasians, Male & Asians, Female & Caucasians, etc.

I figured out the "by" command would be useful in doing this.

cor <- by(data[,c(6,7)],list(data$sex,data$ethnicity),cor)

> cor
: Female
: African American
           wt      ht
wt  1.0000000 0.6879572
ht  0.6879572 1.0000000
------------------------------------------------------------------------------------------------------------------------------------------------ 
: Male
: African American
           wt      ht
wt  1.0000000 0.6868178
ht  0.6868178 1.0000000
------------------------------------------------------------------------------------------------------------------------------------------------ 
: Female
: Hispanic
           wt      ht
wt  1.0000000 0.6162962
ht  0.6162962 1.0000000
------------------------------------------------------------------------------------------------------------------------------------------------ 
: Male
: Hispanic
           wt      ht
wt  1.0000000 0.5854748
ht  0.5854748 1.0000000

My question is, I'd like to convert these results, which are in list form, into a data frame with the following columns:

R2_value Sex Ethnicity

My question is how do I pull the values of sex and ethnicity from the list "cor" (my output from the "by" command).

When I do "cor[[1]]", I just get the correlation matrix for Female, African American.

Calling "names(cor)" doesn't give me what I want either.

This is a first attempt to solve this problem. I'm not sure if the output will work because you haven't expressed how you have your data originally. Please try the code and report your result!

set.seed(123)
# Coerce your data.frame to a data.table
require("data.table")
DT <- data.table(data)
DT <- data.table(wt = rnorm(100, 80, 10), 
                 ht = rnorm(100, 110, 10),
                 sex = factor(sample(c("MALE", "FEMALE"), 100, replace = T)),
                 ethnicity = factor(sample(c("African American", "Hispanic"), 100, replace = T))
                 )

str(DT)

DT[, cor(wt,ht), by="sex,ethnicity"]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM