0

I am not able to filter based on 2 condition. as1 is a dataframe

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")

I need below dataframe

as2
                   da cat
1 2016-06-04 04:05:45   A
Dev P
  • 449
  • 3
  • 12
  • What does `class(as1$da)` say? – Tim Biegeleisen Oct 04 '19 at 13:16
  • its ````[1] "POSIXct" "POSIXt" ```` – Dev P Oct 04 '19 at 13:16
  • 1
    This seems like a typographical error, you have conditioned on `cat == "A"`, but not conditioned on `da`, you could add `da == as.POS...` – bouncyball Oct 04 '19 at 13:19
  • Possible duplicate of [R dplyr - filter by multiple conditions](https://stackoverflow.com/questions/51041946/r-dplyr-filter-by-multiple-conditions) – Matt Summersgill Oct 04 '19 at 13:40
  • Yeah Sorry got it. :) Actually that in R Studio. If I run the same code for filtering purpose in Flexdashboard. It is not working. This is the code ````as2 <- as1 %>% filter(variable == input$b & Date == as.POSIXct(input$s2))```` Here input$b is a filter in cat and Input$s2 is a filter in Date. – Dev P Oct 04 '19 at 14:04
  • What wrong I am doing here in the code. Why filtering on date is not working here. It is same logic right. I have only replaced input$b and input$s2 thats it – Dev P Oct 04 '19 at 14:06
  • A problem (not necessarily THE problem) is that `&&` is not vectorised. It returns a single logical value. You should use `&` or, better yet, write your query as `[...] %>% dplyr::filter(as.POSIXct("2016-06-04 04:05:45"), cat == "A")` – alan ocallaghan Oct 04 '19 at 15:28

2 Answers2

0

Let's make some reproducible data as your question is missing it:

as1 <- read.csv(header = T, text = "
da, cat
2016-06-04 04:05:45,A
2016-06-04 04:05:46,B
2016-06-04 04:05:45,C
2016-06-04 04:05:46,D", stringsAsFactors = FALSE)

Now first thing you want to check is if the column "da" is, in fact, POSIXct.

class(as1$da)
#> [1] "character"

In my sample it is not, so I add an extra line to the dplyr pipe.

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")

Basically what you did wrong was leaving as.POSIXct("2016-06-04 04:05:45") as the expression. filter evaluates a condition, meaning it only keeps the rows where something is TRUE. Hence to "2016-06-04 04:05:45" you need a test---da == as.POSIXct("2016-06-04 04:05:45").

For why you need & here and not &&, see this answer.

JBGruber
  • 11,727
  • 1
  • 23
  • 45
  • 1
    you use `cat` in your filter instead of `as1$cat` and also the `&` can be replaced with a `,` . Just a fyi – Mike Oct 04 '19 at 14:10
  • Thanks. I corrected the `as1$cat` bit. Thanks also for pointing out the `,` works as well. – JBGruber Oct 04 '19 at 15:21
0

You were almost there This is a possible solution for you. You needed to format the data using lubridate before filtering the data.

# 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

Your data

# 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
Not_Dave
  • 491
  • 2
  • 8
  • Thanks. I tried this actually. In your sample dataframe x, the date is in character type so your converting to ````[1] "POSIXct" "POSIXt" ````. In my dataframe, Date is by default in ````[1] "POSIXct" "POSIXt" ```` so there is no need to convert right? – Dev P Oct 04 '19 at 14:16
  • this is the dataframe ````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))```` – Dev P Oct 04 '19 at 14:17
  • Try it without and see if it works. Else convert it and they try. Either case, it should work. Cheers! – Not_Dave Oct 04 '19 at 14:17