是否可以从列中索引出 NA?
Is it possible to index out NA's from a column?
我正在 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
尝试:
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")
我正在 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
尝试:
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")