gpt4 book ai didi

r - dplyr 值汇总返回 NA

转载 作者:行者123 更新时间:2023-12-04 02:16:02 26 4
gpt4 key购买 nike

我试图总结下面的数据框,但我一直得到 NA 而不是我期望的平均值。有人可以解释为什么会这样吗?

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)

最佳答案

我们不需要引用 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

注意:我使用了 na.rm=TRUE删除 NA值(如果存在)。

关于r - dplyr 值汇总返回 NA,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33580210/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com