我正在嘗試遍歷 2 個串列(實際上是 2 個資料集)并對它們進行列統計比較,并按列回傳結果。
我正在嘗試使用 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)
謝謝你的幫助!
uj5u.com熱心網友回復:
如果我理解正確,你想要這個:
output <- purrr::map2(
.x = predat,
.y = postdat,
.f = test_out
)
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/356514.html
