是否可以从列中索引NA?

时间:2018-12-18 09:41:05

标签: r indexing subset data-analysis na

我在RStudio中使用R,并对芝加哥的犯罪行为进行了一些分析。我的数据集中有一个日期列,我将其分为3列(年,月,日)。现在,我想看看每年哪个月发生了多少次犯罪。我的代码和过滤工作正常,我只在数据集(年,月,日)中的NA列中找到某处。我尽了一切努力使他们摆脱困境,但没有成功。有人不知道如何将它们删除吗,还是可以通过索引将它们删除?例如这样的Year[-NA]

这是我的代码的样子:

library(dplyr)
library(highcharter)
library(xts)
library(tidyverse)
library(ggplot2)
library(viridis)

homicide <- cc[cc$Primary.Type == "HOMICIDE",]
homicideAnalysis <- homicide %>% group_by(Year, Month) %>% summarise(Total = n())

ggplot(homicideAnalysis, aes(Year, Month, fill = Total)) +
  geom_tile(size = 1, color = "white") +
  scale_fill_viridis()  +
  geom_text(aes(label = Total), color='white') +
  ggtitle("Homicides in Chicago") 

这是该图的屏幕截图,您可以在其中看到月份和年份的NA:

Homicides in Chicago Plot

PS:unique(cc$Year)给了我这个输出

 [1] 04   03   01   02   <NA> 06   05   07   08   09   11   10   16   15   12   14  
[17] 13   17  
Levels: 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17

1 个答案:

答案 0 :(得分:1)

尝试:

ggplot(homicideAnalysis %>% na.omit(), aes(Year, Month, fill = Total)) +
  geom_tile(size = 1, color = "white") +
  scale_fill_viridis()  +
  geom_text(aes(label = Total), color='white') +
  ggtitle("Homicides in Chicago")