dplyr summarise_each with na.rm

后端 未结 5 1800
伪装坚强ぢ
伪装坚强ぢ 2020-12-12 18:41

Is there a way to instruct dplyr to use summarise_each with na.rm=TRUE? I would like to take the mean of variables with summaris

5条回答
  •  再見小時候
    2020-12-12 19:43

    I don't know if my answer will add something to the previous comments. Hopefully yes.

    In my case, I had a database from an experiment with two groups (control, exp) with different levels for a specific variable (day) and I wanted to get a summary of mean and sd of another variable (weight) for each group for specific levels of the variable day.

    Here is an example of my database:

    animal    group           day       weight      
    1.1       "control"       73        NA   
    1.2       "control"       73        NA   
    3.1       "control"       73        NA   
    9.2       "control"       73        25.2  
    9.3       "control"       73        23.4  
    9.4       "control"       73        25.8   
    2.1       "exp"           73        NA       
    2.2       "exp"           73        NA     
    10.1      "exp"           73        24.4     
    10.2      "exp"           73        NA     
    10.3      "exp"           73        24.6
    

    So, for instance, in this case I wanted to get the mean and sd of the weight on day 73 for each of the groups (control, exp), omitting the NAs.

    I did this with this command:

    data[data$day=="73",] %>% group_by(group) %>% summarise(mean(weight[group == "exp"], na.rm=T),sd(weight[group == "exp"], na.rm=T))
    data[data$day=="73",] %>% group_by(group) %>% summarise(mean(weight[group == "control"], na.rm=T),sd(weight[group == "control"], na.rm=T))
    

提交回复
热议问题