使用厚度较大的padr会导致错误"缺少值,其中需要TRUE / FALSE"

时间:2017-10-10 23:35:13

标签: r padr

我一直试图让padr使用我的数据集但没有取得多大成功,尽管我可以让这些示例工作:

# I have a few datetime columns so I convert all to POSIXct with UTC.  
> df <- mutate_at(DATABASE, vars(ends_with("time")), funs(ymd_hms(., tz = "UTC", locale = Sys.getlocale("LC_TIME"))))
> df <- as_tibble(df)
> head(df, 20)
# A tibble: 20 x 2
             charttime   sbp
                <dttm> <dbl>
 1 2101-10-20 22:30:01    NA
 2 2101-10-20 18:45:00    62
 3 2101-10-20 19:00:00    66
 4 2101-10-20 19:12:00    NA
 5 2101-10-20 19:14:00    NA
 6 2101-10-20 19:15:00   217
 7 2101-10-20 19:26:00    NA
 8 2101-10-20 19:30:00   102
 9 2101-10-20 19:45:00    94
10 2101-10-20 19:59:00    NA
11 2101-10-20 20:00:00    80
12 2101-10-20 20:04:00    NA
13 2101-10-20 20:15:00    91
14 2101-10-20 20:30:00    86
15 2101-10-20 20:45:00    96
16 2101-10-20 21:00:00    73
17 2101-10-20 21:15:00    84
18 2101-10-20 21:30:00    96
19 2101-10-20 21:45:00   100
20 2101-10-20 21:51:00    NA

> df$charttime %>% get_interval # should say 'sec'
[1] "sec"

> df %>% thicken(interval='hour')
Error in if (to_date) x <- as.Date(x, tz = attr(x, "tzone")) : 
  missing value where TRUE/FALSE needed

但是使用padr示例它可以工作:

> coffee %>% thicken(interval='day')
           time_stamp amount time_stamp_day
1 2016-07-07 03:11:21   3.14     2016-07-07
2 2016-07-07 03:46:48   2.98     2016-07-07
3 2016-07-09 07:25:17   4.11     2016-07-09
4 2016-07-10 04:45:11   3.14     2016-07-10
> coffee$time_stamp %>% get_interval # should say 'sec'
[1] "sec"

我还没有弄清楚为什么我的数据集不起作用以及如何解释错误。

更新1

这是我尝试做的另一个更完整的例子。我还包含一个csv,其中包含一小段我正在使用的数据片段,因此这个问题更具可重现性。我在两台机器上试过这个,得到了相同的结果。

您会注意到在上面的示例和下面的示例中,charttime的第一个值是不同的。 (2101-10-20 22:30:01更改为2101-10-20 22:30:00)。我希望将间隔设为&#39; sec&#39;而不是&#39; min&#39;所以我手动更改了值。无论哪种方式都会导致同样的问题。

padr_data.csv

> packageVersion("tidyverse")
[1] ‘1.1.1’
> packageVersion("lubridate")
[1] ‘1.6.0’
> packageVersion("padr")
[1] ‘0.3.0’
> library(tidyverse)
> library(lubridate)
> library(padr)
> 
> df <- read.csv("padr_data.csv")
> df <- mutate_at(df, vars(ends_with("time")), funs(ymd_hms(., tz = "UTC", locale = Sys.getlocale("LC_TIME"))))
> df$sbp <- as.numeric(df$sbp)
> summary(df)
   charttime                        sbp       
 Min.   :2101-10-20 18:30:00   Min.   : 62.0  
 1st Qu.:2101-10-20 19:33:45   1st Qu.: 84.5  
 Median :2101-10-20 20:52:30   Median : 95.0  
 Mean   :2101-10-20 21:08:22   Mean   :100.9  
 3rd Qu.:2101-10-20 22:26:15   3rd Qu.:102.0  
 Max.   :2101-10-21 00:42:00   Max.   :217.0  
                               NA's   :12     
> lapply(df, class)
$charttime
[1] "POSIXct" "POSIXt" 

$sbp
[1] "numeric"

> df$charttime %>% get_interval
[1] "min"
> 
> # this does not work
> df[!is.na(df$charttime),] %>%
+    thicken(interval = 'hour')
Error in if (to_date) x <- as.Date(x, tz = attr(x, "tzone")) : 
  missing value where TRUE/FALSE needed
> 
> # this does not work
> df %>%
+   thicken(interval = 'hour')
Error in if (to_date) x <- as.Date(x, tz = attr(x, "tzone")) : 
  missing value where TRUE/FALSE needed

2 个答案:

答案 0 :(得分:1)

似乎padr与将来设置的日期不兼容!更具体地说,将来超过20年的日期将不起作用。我将向padr开发人员提出一个问题,看看如何改进代码。

> packageVersion("tidyverse")
[1] ‘1.1.1’
> packageVersion("lubridate")
[1] ‘1.6.0’
> packageVersion("padr")
[1] ‘0.3.0’
> library(tidyverse)
> library(lubridate)
> library(padr)
> 
> df <- read.csv("padr_data.csv")
> df <- mutate_at(df, vars(ends_with("time")), funs(ymd_hms(., tz = "UTC", locale = Sys.getlocale("LC_TIME"))- dyears(63)))
> 
> df$sbp <- as.numeric(df$sbp)
> #df <- na.omit(df)
> 
> summary(df)
   charttime                        sbp       
 Min.   :2038-11-04 18:30:00   Min.   : 62.0  
 1st Qu.:2038-11-04 19:33:45   1st Qu.: 84.5  
 Median :2038-11-04 20:52:30   Median : 95.0  
 Mean   :2038-11-04 21:08:22   Mean   :100.9  
 3rd Qu.:2038-11-04 22:26:15   3rd Qu.:102.0  
 Max.   :2038-11-05 00:42:00   Max.   :217.0  
                               NA's   :12     
> lapply(df, class)
$charttime
[1] "POSIXct" "POSIXt" 

$sbp
[1] "numeric"

> df$charttime %>% get_interval
[1] "min"
> 
> # this does not work
> df[!is.na(df$charttime),] %>%
+   thicken(interval = 'hour')
Error in if (to_date) x <- as.Date(x, tz = attr(x, "tzone")) : 
  missing value where TRUE/FALSE needed
In addition: Warning messages:
1: In round_down_core(a, b) : NAs introduced by coercion to integer range
2: In round_down_core(a, b) : NAs introduced by coercion to integer range

dyears(63)更改为dyears(64)

> df <- mutate_at(df, vars(ends_with("time")), funs(ymd_hms(., tz = "UTC", locale = Sys.getlocale("LC_TIME"))- dyears(64)))
> 
> df$sbp <- as.numeric(df$sbp)
> #df <- na.omit(df)
> 
> summary(df)
   charttime                        sbp       
 Min.   :2037-11-04 18:30:00   Min.   : 62.0  
 1st Qu.:2037-11-04 19:33:45   1st Qu.: 84.5  
 Median :2037-11-04 20:52:30   Median : 95.0  
 Mean   :2037-11-04 21:08:22   Mean   :100.9  
 3rd Qu.:2037-11-04 22:26:15   3rd Qu.:102.0  
 Max.   :2037-11-05 00:42:00   Max.   :217.0  
                               NA's   :12     
> lapply(df, class)
$charttime
[1] "POSIXct" "POSIXt" 

$sbp
[1] "numeric"

> df$charttime %>% get_interval
[1] "min"
> 
> # this does work
> df[!is.na(df$charttime),] %>%
+   thicken(interval = 'hour')
             charttime sbp      charttime_hour
1  2037-11-04 18:30:00  NA 2037-11-04 18:00:00
2  2037-11-04 18:45:00  62 2037-11-04 18:00:00
3  2037-11-04 19:00:00  66 2037-11-04 19:00:00
4  2037-11-04 19:12:00  NA 2037-11-04 19:00:00
5  2037-11-04 19:14:00  NA 2037-11-04 19:00:00
6  2037-11-04 19:15:00 217 2037-11-04 19:00:00
7  2037-11-04 19:26:00  NA 2037-11-04 19:00:00
8  2037-11-04 19:30:00 102 2037-11-04 19:00:00
9  2037-11-04 19:45:00  94 2037-11-04 19:00:00
10 2037-11-04 19:59:00  NA 2037-11-04 19:00:00
11 2037-11-04 20:00:00  80 2037-11-04 20:00:00
12 2037-11-04 20:04:00  NA 2037-11-04 20:00:00
13 2037-11-04 20:15:00  91 2037-11-04 20:00:00
14 2037-11-04 20:30:00  86 2037-11-04 20:00:00
15 2037-11-04 20:45:00  96 2037-11-04 20:00:00
16 2037-11-04 21:00:00  73 2037-11-04 21:00:00
17 2037-11-04 21:15:00  84 2037-11-04 21:00:00
18 2037-11-04 21:30:00  96 2037-11-04 21:00:00
19 2037-11-04 21:45:00 100 2037-11-04 21:00:00
20 2037-11-04 21:51:00  NA 2037-11-04 21:00:00
21 2037-11-04 22:00:00  NA 2037-11-04 22:00:00
22 2037-11-04 22:15:00 123 2037-11-04 22:00:00
23 2037-11-04 22:30:00 125 2037-11-04 22:00:00
24 2037-11-04 22:45:00 132 2037-11-04 22:00:00
25 2037-11-04 23:00:00  88 2037-11-04 23:00:00
26 2037-11-04 23:15:00  NA 2037-11-04 23:00:00
27 2037-11-04 23:45:00  NA 2037-11-04 23:00:00
28 2037-11-05 00:00:00 102 2037-11-05 00:00:00
29 2037-11-05 00:28:00  NA 2037-11-05 00:00:00
30 2037-11-05 00:42:00  NA 2037-11-05 00:00:00

答案 1 :(得分:0)

解决方案1 ​​ - 没有工作

不太清楚包装,但我会尝试两件事:

  1. 过滤NA值
  2. 声明by参数
  3. 试试这个

    df[!is.na(df$sbp),] %>% thicken(interval='hour', by = 'charttime')
    

    解决方案2 - 没有工作

    尝试将df强制转换为数据框而不是tibble,同时尝试将charttime强制转换为日期:

    df <- data.frame(df)
    df$charttime <- as.POSIXct(df$charttime)
    

    解决方案3 - 没有工作

    NA上可能有一些charttime,请尝试以下操作:

    df[!is.na(df$charttime),] %>% thicken(interval = 'hour')
    

    我尝试重命名变量,但这不是问题。 对不起,我还不能发表评论。请告诉我它是否有效。