简体   繁体   English

dplyr值摘要返回NA

[英]dplyr summary of values returns NA

I am trying to summarize the dataframe below, but I keep getting NA instead of the mean I was expecting. 我正在尝试总结下面的数据框,但是我一直得到NA,而不是我期望的平均值。 Can someone explain why this is happening? 有人可以解释为什么会这样吗?

fbobjective<-data.frame(
  Participant=c("a","c","c"),
  "Part.2.or.3."=c(3,2,2),
  No_Photos=c(10,12,40)
)
fb_obj_v2<-fbobjective
#unique participant id's
fb_obj_v2$Participant<-paste0(fb_obj_v2$Participant,fb_obj_v2$"Part.2.or.3.")
fb_obj_v2$"Part.2.or.3."<-NULL
dim(fb_obj_v2)
#convert numbers to numbers or NA. bind Participant names back to this
numeric_results<-as.matrix(sapply(fb_obj_v2, as.numeric))
numeric_results<-as.data.frame(numeric_results)
numeric_results[,1]<-fb_obj_v2$Participant
numeric_results<-as.data.frame(numeric_results)
View(numeric_results)
#return data
res<-numeric_results %>% group_by(Participant) %>% summarise(mean("No_Photos"))
View(res)

We don't need to quote the No_Photos 我们不需要引用No_Photos

 numeric_results %>% 
        group_by(Participant) %>% 
        summarise( Mean = mean(No_Photos, na.rm=TRUE))
 #  Participant  Mean
 #        (chr) (dbl)
 #1          a3    10
 #2          c2    26

NOTE: I used na.rm=TRUE to remove NA values if present. 注意:我使用na.rm=TRUE删除NA值(如果存在)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM