【问题标题】:use dplyr with missing data使用缺失数据的 dplyr
【发布时间】:2017-01-22 02:47:59
【问题描述】:

再次延续我之前的 2 个问题,但问题略有不同。我一直在处理的数据中的另一个皱纹:

date <- c("2016-03-24","2016-03-24","2016-03-24","2016-03-24","2016-03-24",
          "2016-03-24","2016-03-24","2016-03-24","2016-03-24")
location <- c(1,1,2,2,3,3,4,"out","out")
sensor <- c(1,16,1,16,1,16,1,1,16)
Temp <- c(35,34,92,42,21,47,42,63,12)
df <- data.frame(date,location,sensor,Temp)

我的一些数据有缺失值。 NA 未指明它们。它们只是不在数据周期内。

我想从位置“4”中减去位置“out”,忽略其他位置,我想按日期和传感器来做。我已经成功地使用具有以下代码的所有数据的数据位置完成了此操作

df %>%
  filter(location %in% c(4, 'out')) %>% 
  group_by(date, sensor) %>% 
  summarize(Diff = Temp[location=="4"] - Temp[location=="out"],
            location = first(location)) %>%
  select(1, 2, 4, 3) 

但是对于缺少日期的数据,我收到以下错误Error: expecting a single value。我认为这是因为dplyr 在到达丢失的数据点时不知道该怎么做。

做一些研究,似乎do 是要走的路,但它返回的数据框没有任何相互减去的值。

df %>%
  filter(location %in% c(4, 'out')) %>% 
  group_by(date, sensor) %>% 
  do(Diff = Temp[location=="4"] - Temp[location=="out"],
            location = first(location)) %>%
  select(1, 2, 4, 3) 

如果找不到要减去的条目之一,有没有办法覆盖 dplyr 并告诉它返回 NA

【问题讨论】:

  • 顺便说一句,即使没有丢失日期值,我也会收到同样的数据错误!
  • 有缺失值

标签: r dplyr


【解决方案1】:
library(tidyverse)

date <- c("2016-03-24", "2016-03-24", "2016-03-24", "2016-03-24", "2016-03-24",
          "2016-03-24", "2016-03-24", "2016-03-24", "2016-03-24")
location <- c(1, 1, 2, 2, 3, 3, 4, "out", "out")
sensor <- c(1, 16, 1, 16, 1, 16, 1, 1, 16)
Temp <- c(35, 34, 92, 42, 21, 47, 42, 63, 12)

df <- data_frame(date, location, sensor, Temp)

# edge case helper
`%||0%` <- function (x, y) { if (is.null(x) | length(x) == 0) y else x }

df %>%
  filter(location %in% c(4,  'out')) %>%
  mutate(location=factor(location, levels=c("4", "out"))) %>%             # make location a factor 
  arrange(sensor, location) %>%                                           # order it so we can use diff()
  group_by(date,  sensor) %>%
  summarize(Diff = diff(Temp) %||0% NA, location = first(location)) %>% # deal with the edge case
  select(1,  2,  4,  3)
## Source: local data frame [2 x 4]
## Groups: date [1]
## 
##         date sensor location  Diff
##        <chr>  <dbl>   <fctr> <dbl>
## 1 2016-03-24      1        4    21
## 2 2016-03-24     16      out    NA

【讨论】:

    【解决方案2】:

    如果我们想返回NA,可能的选项是

    library(dplyr)
    df %>%
        filter(location %in% c(4, 'out')) %>% 
        group_by(date, sensor) %>%
        arrange(sensor, location) %>%   
        summarise(Diff = if(n()==1) NA else diff(Temp), location = first(location))  %>%
        select(1, 2, 4, 3)
    #        date sensor location  Diff
    #      <fctr>  <dbl>   <fctr> <dbl>
    #1 2016-03-24      1        4    21
    #2 2016-03-24     16      out    NA
    

    data.table 中的等效选项是

    library(data.table)
    setDT(df)[location %in% c(4, 'out')][
         order(sensor, location), .(Diff = if(.N==1) NA_real_ else diff(Temp), 
          location = location[1]), .(date, sensor)][, c(1, 2, 4, 3), with = FALSE]
    #          date sensor location Diff
    #1: 2016-03-24      1        4   21
    #2: 2016-03-24     16      out   NA
    

    【讨论】:

      猜你喜欢
      • 2020-05-05
      • 2021-09-03
      • 2021-11-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-28
      • 2015-10-06
      • 2017-01-24
      相关资源
      最近更新 更多