我需要关于这个数据库https://www.kaggle.com/datasets/hugomathien/soccer的帮助,我想找出有多少玩家是右脚的,有多少是左脚的,使用数据库表player_attributes的列preferred_foot,并使用: group_by和dplyr的汇总。当我在r中运行这个时:
con <- DBI::dbConnect(RSQLite::SQLite(), "data/database.sqlite")
library(tidyverse)
library(DBI)
player_attributes<-tbl(con,"Player_Attributes")
Table_preferred_foot<- player_attributes %>%
group_by(preferred_foot) %>%
summarize(number_of_players=count(preferred_foot))
head(Table_preferred_foot)
我得到了右脚和左脚球员的数量,我也得到了NA的数目是0。但如果我跑了
player_attributes %>%
group_by(preferred_foot) %>%
count()
我得到了右脚和左脚球员的数量(和以前一样),但我得到NA的数目是836,这是NA的真实数量。如何使用汇总和group_by得到正确的答案?
此外,是否有一个直接函数来检查惰性查询的变量中是否存在NA,并从惰性查询的变量(如常规数据帧)中删除NA??(像na.omit()这样的基本函数不起作用)
发布于 2022-05-21 02:15:43
您可以在每个代码段1中使用group_by
和summarise
。Count
将其合并成每段2的一行,并且可以将每个片段3中的NAs计算出来。
library(tidyverse)
con <- DBI::dbConnect(RSQLite::SQLite(), "database.sqlite")
tbl(con, "Player_Attributes") %>%
group_by(preferred_foot) %>%
summarise(n = n())
tbl(con, "Player_Attributes") %>%
count(preferred_foot)
tbl(con, "Player_Attributes") %>%
filter(!is.na(preferred_foot)) %>%
count(preferred_foot)
https://stackoverflow.com/questions/72325671
复制