按向量过滤日期时间

问题描述 投票:1回答:1

这可能真的很简单。在第一种情况下,使用总统数据,我可以按年或年2进行筛选。结果相同。

但是,当我使用posixct数据并尝试以类似方式进行过滤时,我遇到了问题。

我写的时候

school_hours2<-as.character(c(07:18))

我可以看到school_hours 2中的值是“ 7”,“ 8”,“ 9”等而在他们是school_hours“ 07”“ 08”“ 09”等

编辑:我想这解释了那区别吗?编辑:我可以看到比较integer:character的问题,甚至当我将向量as.character写入时,向量中的值也与我想要的不匹配。

我希望能够按school_hours2进行过滤。那样就意味着我可以认为“我想在这两次之间进行过滤”并将上限和下限放入其中。而不必在它们之间写下所有间隔点。我怎么得到这个?

为什么用“ Y”过滤比用“ H”过滤容易?

library (tidyverse)
#some data - filtering works
data(presidential)
head(presidential)
str(presidential)
presidential%>%filter(format(as.Date(start),"%Y")<=2005)
years<-c('1979', '1980', '1981', '1982', 
                '1983', '1984', '1985', '1986',
                '1987', '1988', '1989', '1990'
)
years2<-c(1950:1990)
presidential%>%filter(format(as.Date(start),"%Y")%in% years2)
presidential%>%filter(format(as.Date(start),"%Y")%in% years)


#some date time data - filtering.
test_data<-sample(seq(as.POSIXct('2013/01/01'), as.POSIXct('2017/05/01'), by="day"), 1000)
td<-as.data.frame(test_data)%>%mutate(id = row_number())

school_hours<-c('07', '08', '09', '10', 
                '11', '12', '13', '14',
                '15', '16', '17', '18'
                  )
school_hours2<-c(07:18)
school_years<-c(2015,2016,2017)
school_years2<-c(2015:2017)

str(td)
test1<-td%>%
  filter(id >=79)

schools<-td%>%
  filter(format(test_data,'%H') %in% school_hours)

schools2<-td%>%
  filter(format(test_data,'%H') %in% school_hours2)

schools3<-td%>%
  filter(format(test_data,'%Y')==2017)

schools4<-td%>%
  filter(format(test_data,'%Y') %in% school_years)

schools5<-td%>%
  filter(format(test_data,'%Y') %in% school_years2)

这是我的问题:在上面的代码中,当我尝试使用school_hours或school_hours2筛选td(包含posixct数据)时,返回的数据为零。为什么?

我想做的是代替写作

school_hours<-c('07', '08', '09', '10', 
                    '11', '12', '13', '14',
                    '15', '16', '17', '18'
                      )

我会写

school_hours2<-c(07:18)

就像我在school_years一样,过滤将起作用。这不起作用

schools2<-td%>%
  filter(format(test_data,'%H') %in% school_hours2)

这确实有效

schools5<-td%>%
  filter(format(test_data,'%Y') %in% school_years2)

为什么?

我问是因为:我使用了类似的方法来过滤我无法共享的真实数据,但出现了差异。

[当我使用school_hours(这是一个字符)时,我生成993条记录,第一次是07:00。当我使用school_hours2(是整数)时,我生成895条记录,第一次是10:00。我知道-“没有数据,我们无法进行任何评估”,但是我无法解决的是为什么两个不同的向量滤波器的工作方式不同。是否因为school_hours包含字符和school_hours2整数?

编辑:我将test_data行更改为:

#some date time data - filtering.
test_data<-as.POSIXct(sample(seq(1127056501, 1127056501), 1000),origin = "1899-12-31",tz="UTC")

仍然有问题:

schools<-td%>%
  filter(format(test_data,'%H') %in% school_hours)

产生510行

schools2<-td%>%
  filter(format(test_data,'%H') %in% school_hours2)

产生379行

我真正感兴趣的所有数据看起来像这样1899-12-31 23:59:00

((后6位代表24小时制)

我真正想做的就是从此转换时间1899-12-31 07:59:00至小时(7)

然后

使用

school_hours2<-c(07:18)

作为过滤器。但是将由转换产生的小时1899-12-31 07:59:00

是07要么7

因为如果是07,那么school_hours2

我该如何解决?

编辑:像这样:R: how to filter a timestamp by hour and minute?

td1<-td%>%mutate(timestamp_utc = ymd_hms(test_data,tz="UTC"))%>%
  mutate(hour = hour(timestamp_utc))%>%
filter(hour(timestamp_utc) %in% school_hours)

td2<-td%>%mutate(timestamp_utc = ymd_hms(test_data,tz="UTC"))%>%
  mutate(hour = hour(timestamp_utc))%>%
  filter(hour(timestamp_utc) %in% school_hours2)

td3<-td%>%
  mutate(hour = hour(test_data))%>%
  filter(hour(test_data) %in% school_hours2)
r datetime tidyverse posixct
1个回答
0
投票

经过很多混蛋并在我的问题中与自己交谈之后

我发现了这个线程:filtering a dataset by time stamp

并且它帮助我认识到如何隔离时间戳中的小时,然后使用它来正确过滤数据。

最后的答案是通过此隔离小时

filter(hour(timestamp_utc) %in% school_hours2)
© www.soinside.com 2019 - 2024. All rights reserved.