Home > Blockchain >  Unlist and access multiple lists in R
Unlist and access multiple lists in R

Time:11-02

I have a dataset in R and do some transformations in order to calculate the maximum information coefficient for the pairs (Y,X1) and (Y,X2) as follows:

dat <- as.data.frame(matrix(rnorm(100 * 5, mean = 0, sd = 1), 10, 5))
colnames(dat) <- c("Y",paste0("X",1:4))
dat$X1 <- 0.5*dat$Y 
dat$X2 <-  dat$Y^2
library(minerva)
X1res <- mine(dat$X1,dat$Y)$`MIC-R2` 
X2res <- mine(dat$X2,dat$Y)$`MIC-R2` 

Then, i check the following statements:

res1 <- ifelse(isTRUE(X1res <= 0.4),1,0)
res2 <- ifelse(isTRUE(X2res >= 0.6),1,0)
res3 <- ifelse(isTRUE(X1res <= 0.4 & X2res >= 0.6),1,0)

Finally i replicate the procedure 10 times as follows:

   res <- replicate(10, {
dat <- as.data.frame(matrix(rnorm(100 * 5, mean = 0, sd = 1), 10, 5))
colnames(dat) <- c("Y",paste0("X",1:4))
dat$X1 <- 0.5*dat$Y 
dat$X2 <-  dat$Y^2
library(minerva)
X1res <- mine(dat$X1,dat$Y)$`MIC-R2` 
X2res <- mine(dat$X2,dat$Y)$`MIC-R2` 

res1 <- ifelse(isTRUE(X1res <= 0.4),1,0)
res2 <- ifelse(isTRUE(X2res >= 0.6),1,0)
res3 <- ifelse(isTRUE(X1res <= 0.4 & X2res >= 0.6),1,0)
dplyr::lst(res1, res2, res3)

}, simplify = FALSE)

How can I take from the resulting lists the percentages of the above 3 statements, i.e., res1 = 20%, res2 = 70% and res3 = 20%

CodePudding user response:

m1 <- do.call(cbind, lapply(res, \(x) do.call(rbind, x)))
rowMeans(m1)* 100

library(dplyr)
bind_rows(res) %>% 
  summarise(across(everything(), ~ 100 * mean(.x)))
  • Related