R - 遍历 2 个列表和 return 个列表
R - iterating over 2 lists and return a list
我正在尝试遍历 2 个列表(实际上是 2 个数据集)并按列对它们进行统计比较,return 结果按列进行。
我正在尝试使用 lapply 来执行此操作,但我无法获得正确的语法。这是我的代码的一些示例数据:
### predat and postdat are the datasets to be compared columnwise
predat<- as.data.frame(matrix(data = rnorm(25), nrow = 25, ncol = 5))
postdat<-as.data.frame(matrix(data = rnorm(25), nrow = 25, ncol = 5))
colnames(predat)<-c("x1","x2","x3","x4","x5")
colnames(postdat)<-c("y1","y2","y3","y4","y5")
predat<-as.list(predat)
postdat<-as.list(postdat)
test_out<-function(x,y){
res<-wilcox.test(x,y, paired = TRUE, alternative = "two.sided")
return(res)
}
## I want the results of comparing predat and postdat columnwise in a list
out_all<-lapply(predat,postdat, test_out)
感谢您的帮助!
如果我没理解错的话,你想要这个:
output <- purrr::map2(
.x = predat,
.y = postdat,
.f = test_out
)
我正在尝试遍历 2 个列表(实际上是 2 个数据集)并按列对它们进行统计比较,return 结果按列进行。
我正在尝试使用 lapply 来执行此操作,但我无法获得正确的语法。这是我的代码的一些示例数据:
### predat and postdat are the datasets to be compared columnwise
predat<- as.data.frame(matrix(data = rnorm(25), nrow = 25, ncol = 5))
postdat<-as.data.frame(matrix(data = rnorm(25), nrow = 25, ncol = 5))
colnames(predat)<-c("x1","x2","x3","x4","x5")
colnames(postdat)<-c("y1","y2","y3","y4","y5")
predat<-as.list(predat)
postdat<-as.list(postdat)
test_out<-function(x,y){
res<-wilcox.test(x,y, paired = TRUE, alternative = "two.sided")
return(res)
}
## I want the results of comparing predat and postdat columnwise in a list
out_all<-lapply(predat,postdat, test_out)
感谢您的帮助!
如果我没理解错的话,你想要这个:
output <- purrr::map2(
.x = predat,
.y = postdat,
.f = test_out
)