handling NA values in apply functions returning more than one value

前端 未结 1 1513
梦谈多话
梦谈多话 2021-01-13 23:13

I have dataframe df with two columns col1, col2, includes NA values in them. I have to calculate mean,

相关标签:
1条回答
  • 2021-01-14 00:05

    Here is an option:

    funs <- list(sd=sd, mean=mean)
    sapply(funs, function(x) sapply(df, x, na.rm=T))
    

    Produces:

               sd       mean    
    col1.value 39.34826 39.42857
    col2.value 28.33946 51.625  
    

    If you want to get cute with the functional library:

    sapply(funs, Curry(sapply, X=df), na.rm=T)
    

    Does the same thing.

    0 讨论(0)
提交回复
热议问题