【发布时间】:2026-02-15 07:10:01
【问题描述】:
我有 3 个数据帧(A、B1 和 B2)。我 split 每个变量 study.name 得到我的期望输出 显示为out1, out2, out3:
J <- split(A, A$study.name); out1 <- do.call(rbind, c(J, make.row.names = F))
M <- split(B1, B1$study.name); out2 <- do.call(rbind, c(M, make.row.names = F))
N <- split(B2, B2$study.name); out3 <- do.call(rbind, c(N, make.row.names = F))
但我想知道为什么我的函数foo 无法实现相同的输出? (见下文)
A <- read.csv("https://raw.githubusercontent.com/izeh/m/master/irr.csv", h = T) ## data A
B1 <- read.csv('https://raw.githubusercontent.com/izeh/m/master/irr2.csv', h = T) ## data B1
B2 <- read.csv("https://raw.githubusercontent.com/izeh/m/master/irr4.csv", h = T) ## data B2
foo <- function(...){ ## The unsuccessful function `foo`
r <- list(...)
## r <- Can we HERE delete rows and columns that are ALL `NA` or EMPTY in `r`?
J <- unlist(lapply(seq_along(r), function(i) split(r[[i]], r[[i]]$study.name)), recursive = FALSE)
lapply(seq_along(J), function(i)do.call(rbind, c(J[[i]], make.row.names = FALSE)) )
}
foo(B1, B2) # Example without success
【问题讨论】:
标签: r list function loops dataframe