Using shapiro.test on multiple columns in a data frame

前端 未结 3 2014
伪装坚强ぢ
伪装坚强ぢ 2020-12-28 10:08

It seems like a pretty simple question, but I can\'t find the answer.

I have a dataframe (lets call it df), containing n=100 columns (C1, <

3条回答
  •  悲&欢浪女
    2020-12-28 10:39

    Use do.call with rbind and lapply for more simple and compact solution:

    df <- data.frame(a = rnorm(100), b = rnorm(100), c = rnorm(100))
    do.call(rbind, lapply(df, function(x) shapiro.test(x)[c("statistic", "p.value")]))
    #>   statistic p.value    
    #> a 0.986224  0.3875904  
    #> b 0.9894938 0.6238027
    #> c 0.9652532 0.009694794
    

提交回复
热议问题