【问题标题】:Is it possible to index out NA's from a column?是否可以从列中索引出 NA?
【发布时间】:2019-05-18 16:49:53
【问题描述】:

我在 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

【问题讨论】:

    标签: r indexing subset data-analysis na


    【解决方案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") 
    

    【讨论】:

    • 不客气!如果有帮助,请考虑接受/支持答案。
    猜你喜欢
    • 1970-01-01
    • 2020-01-29
    • 2011-11-09
    • 2011-08-18
    • 1970-01-01
    • 1970-01-01
    • 2020-04-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多