如何根据2种条件进行过滤

时间:2019-10-04 13:13:29

标签: r

我无法根据2个条件进行过滤。 as1是一个数据框

as1
                   da cat
1 2016-06-04 04:05:45   A
2 2016-06-04 04:05:46   B
3 2016-06-04 04:05:45   C
4 2016-06-04 04:05:46   D

as2 <- as1 %>% filter(as.POSIXct("2016-06-04 04:05:45") && cat == "A")

我需要在数据框下方

as2
                   da cat
1 2016-06-04 04:05:45   A

2 个答案:

答案 0 :(得分:0)

让我们提供一些可重现的数据,因为您的问题缺少它:

POSIXct

现在您要检查的第一件事是“ da”列实际上是否为class(as1$da) #> [1] "character"

dplyr

在我的示例中不是,所以我在library(dplyr) as2 <- as1 %>% mutate(da = as.POSIXct(da)) %>% # add only if column isn't POSIXct filter(da == as.POSIXct("2016-06-04 04:05:45") & cat == "A") 管道中添加了一行。

as.POSIXct("2016-06-04 04:05:45")

基本上,您做错的是保留filter作为表达式。 TRUE评估条件,这意味着它仅保留 something da == as.POSIXct("2016-06-04 04:05:45")的行。因此,对于“ 2016-06-04 04:05:45”,您需要进行测试--- &

关于为什么在这里需要&&而不是DROP TABLE IF EXISTS ##Test DECLARE @tbl_query as NVARCHAR(MAX) = 'CREATE TABLE ##Test ( ' DECLARE @tablecol VARCHAR(300), @tablettype VARCHAR(300) , @typelengt VARCHAR(300) DECLARE cursor_product CURSOR FOR sELECT c.name 'Column Name', t.Name 'Data type' , IIF(t.name = 'nvarchar', c.max_length / 2, c.max_length) FROM sys.columns c INNER JOIN sys.types t ON c.user_type_id = t.user_type_id LEFT OUTER JOIN sys.index_columns ic ON ic.object_id = c.object_id AND ic.column_id = c.column_id LEFT OUTER JOIN sys.indexes i ON ic.object_id = i.object_id AND ic.index_id = i.index_id WHERE c.object_id = OBJECT_ID('your_table_name') and ISNULL(i.is_primary_key, 0)=1 OPEN cursor_product; FETCH NEXT FROM cursor_product INTO @tablecol, @tablettype, @typelengt WHILE @@FETCH_STATUS = 0 BEGIN IF @tablettype LIKE '%varchar%' BEGIN SET @typelengt = '(' + @typelengt + ')' END ELSE BEGIN SET @typelengt='' END set @tbl_query = @tbl_query + @tablecol + ' ' + @tablettype + @typelengt + ' , ' FETCH NEXT FROM cursor_product INTO @tablecol, @tablettype, @typelengt END; CLOSE cursor_product; DEALLOCATE cursor_product; SET @tbl_query = SUBSTRING(@tbl_query,1,LEN(@tbl_query)-1) SET @tbl_query = @tbl_query + ' )' PRINT @tbl_query EXEC sp_executesql @tbl_query 的原因,请参见this answer

答案 1 :(得分:0)

您几乎在那儿这是您可能的解决方案。您需要在过滤数据之前使用lubridate格式化数据。

# load library
library(dplyr)

# create data
x = data.frame(da = c("2019-10-04 07:05:02","2019-10-04 07:05:03","2019-10-04 07:05:02","2019-10-04 07:05:03","2019-10-04 07:05:04"), 
               db = c("a","a","c","a","a"), stringsAsFactors = F)

# convert to date time format
x$da = lubridate::ymd_hms(x$da)

# see the structure of data
str(x)

# filter the data
x %>% filter(da <= lubridate::ymd_hms('2019-10-04 07:05:02') & db == 'a' )

#                   da db
#1 2019-10-04 07:05:02  a

您的数据

# Data
x = structure(list(da = structure(c(1464993345, 1464993346, 1464993345, 1464993346), class = c("POSIXct", "POSIXt"), tzone = ""), cat = structure(1:4, .Label = c("A", "B", "C", "D"), class = "factor")), class = "data.frame", row.names = c(NA, -4L))

# convert to date time format
x$da = lubridate::ymd_hms(x$da)

# see the structure of data
str(x)

# filter the data
x %>% filter(da <= lubridate::ymd_hms('2016-06-03 15:35:45') & cat == 'A' )

#                   da cat
#1 2016-06-03 15:35:45   A