为什么即使在 R 中使用 dplyr 将 na.rm 设置为 TRUE 也是 NaN?
如果na.rm使用dplyr包设置为TRUE,则统计操作的输出将返回NaN。为了避免这种情况,我们需要排除na.rm。按照以下步骤了解tw之间的区别-
首先,创建一个数据框。
na.rm如果数据框中存在NA,则将数据框汇总为TRUE。
汇总数据框而不设置na.rm为TRUE。
创建数据框
让我们创建一个数据框,如下所示-
例子
Group&li;-rep(c("First","Second","Third"),times=c(3,10,7)) Response&li;-rep(c(NA,3,4,5,7,8),times=c(3,2,5,2,4,4)) df&li;-data.frame(Group,Response) df
执行时,上述脚本生成以下内容output(thisoutputwillvaryonyoursystemduetorandomization)-
输出
Group Response 1 First NA 2 First NA 3 First NA 4 Second 3 5 Second 3 6 Second 4 7 Second 4 8 Second 4 9 Second 4 10 Second 4 11 Second 5 12 Second 5 13 Second 7 14 Third 7 15 Third 7 16 Third 7 17 Third 8 18 Third 8 19 Third 8 20 Third 8
汇总数据框并na.rm设置为TRUE
加载dplyr包并用每组响应的平均值汇总数据框df-
例子
library(dplyr) Group<-rep(c("First","Second","Third"),times=c(3,10,7)) Response<-rep(c(NA,3,4,5,7,8),times=c(3,2,5,2,4,4)) df<-data.frame(Group,Response) df%>%group_by(Group)%>%summarise(mean=mean(Response,na.rm=TRUE))
输出
# A tibble: 3 x 2 Group mean1 First NaN 2 Second 4.3 3 Third 7.57
汇总数据框而不设置na.rm为TRUE
用每组响应的平均值总结数据帧df而不设置na.rm为TRUE-
例子
Group<-rep(c("First","Second","Third"),times=c(3,10,7)) Response<-rep(c(NA,3,4,5,7,8),times=c(3,2,5,2,4,4)) df<-data.frame(Group,Response) df%>%group_by(Group)%>%summarise(mean=mean(Response))
输出
# A tibble: 3 x 2 Group mean1 First NA 2 Second 4.3 3 Third 7.57