Without data it's difficult to say but maybe the following untested solution will do what the question asks for.
num <- which(sapply(DF[3:41], is.numeric))
num <- intersect(3:41, num)
do.call(
rbind.data.frame,
lapply(DF[num], function(x){
tryCatch(shapiro.test(x)[c("statistic", "p.value")],
error = function(e) e)
})
)
Edit
If some of the tests return an error, the lapply
instruction will return different types of data and the rbind.data.frame
method will also give an error. The following code solves that problem by saving the lapply
results in a named list, test_list
and checking the list members for errors before binding the right ones.
test_list <- lapply(DF[num], function(x){
tryCatch(shapiro.test(x)[c("statistic", "p.value")],
error = function(e) e)
})
err <- sapply(test_list, inherits, "error")
err_list <- test_list[err]
do.call(rbind.data.frame, test_list[!err])
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…