R Kruskal-Wallis with multiple factors

。_饼干妹妹 提交于 2021-02-16 14:52:38

问题


I'm looking for help on performing the Kruskal-Wallis test on my set of data for a large number of factors. I can perform the test for a single factor, like AD_1yr:

kruskal.test(Shannon ~ AD_1y, data=comm)

But I have over 50 factors I want to test, and was hoping there is a code I can enter that will perform the test for all the factors without having to manually perform the test 50 different times.


回答1:


We can use lapply to loop over the factor columns, create a data.frame with the 'shannon' column and do the kruskal.test

allfactorcolumns <- sapply(comm, is.factor)
lst <- lapply(comm[allfactorcolumns], function(x) 
    kruskal.test(Shannon~., data= data.frame(x, comm['Shannon'])))

If we need to extract the 'p.value', 'df', etc.

do.call(rbind, lapply(lst, function(x) data.frame(Pval= x$p.value, 
                     stat= x$statistic, df= x$parameter)))


来源:https://stackoverflow.com/questions/35091164/r-kruskal-wallis-with-multiple-factors

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