Submit every similarly named elements of a list of vectors to a function in R

梦想的初衷 提交于 2020-08-05 13:09:35

问题


Below, I'm wondering how to use BASE R function quantile() separately across elements in L that are named EFL and ESL?

Note: this is a toy example, L could contain any number of similarly named elements.

foo <- function(X) {
  X <- as.matrix(X)
  tab <- table(row(X), factor(X, levels = sort(unique(as.vector(X)))))
  w <- diag(ncol(tab))
  rosum <- rowSums(tab)
  obs_oc <- tab * (t(w %*% t(tab)) - 1)
  obs_c <- colSums(obs_oc)
  max_oc <- tab * (rosum - 1)
  max_c <- colSums(max_oc)
  SA <- obs_c / max_c
  h <- names(SA)
  h[is.na(h)] <- "NA"
  setNames(SA, h)
}  

DAT <- read.csv("https://raw.githubusercontent.com/rnorouzian/m/master/X.csv", row.names = 1)
L <- replicate(50, foo(DAT[sample(1:nrow(DAT), replace = TRUE),]), simplify = FALSE)

# How to use `quantile()` separately across all similarly named elements (e.g., EFL, ESL) in `L[[i]]` i = 1,... 5

# quantile(all EFL elements across `L`)
# quantile(all ESL elements across `L`)

回答1:


The previous solution I used do.call to rbind each list into a matrix and array and then calculate the quantile over each data.frame row.

sapply(as.data.frame(do.call(rbind, L)), quantile)

However, when there is a missing row, it does not take that into account. To accurately get the rows you need to fill the missing rows. I used data.table's rbindlist (you could also use plyr::rbind.fill) with fill=TRUE to fill the missing values. It requires each to be a data.frame/table/list, so I converted each to a data.frame, but before doing so you need to transpose (t()) the data so that the rows line up to each element. It could be written in a single line, but it's easier read what is happening in multiple lines.

L2 = lapply(L, function(x){as.data.frame(t(x))})
df = data.table::rbindlist(L2, fill=TRUE) # or plyr::rbind.fill(L2)
sapply(df, quantile, na.rm = TRUE)



回答2:


You can also use purrr::transpose:

Lt <- purrr::tranpose(L)
quantile(unlist(Lt$EFL),.8)
quantile(unlist(Lt$ESL),.8)


来源:https://stackoverflow.com/questions/62983117/submit-every-similarly-named-elements-of-a-list-of-vectors-to-a-function-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!