Tristan Tran
Tristan Tran

Reputation: 1513

dplyr filter data.frame with multiple criteria

I have data frame with columns. I want to return a sub-data frame that is filtered off from the original one by multiple conditions. My first questionis that I want to filter by two of the columns: One column contains discrete values and i want to filter rows that contain the value "Open". The other column contain date values and i want to compound-filter for rows that is equal to a given date

This is my filtering function, using dplyr

agendaReq <- function(recTable, agendaDate)
{

  openTable <- filter(recTable, recTable$reqStatus == "Open") 
  agendaTable <- filter(openTable, recTable$reqDate == agendaDate)
  return (agendaTable)

}

However, when I call this function in my server as following

agendaToShow <- agendaReq(recordTable_CA, Sys.Date())

I got this error

Warning: Error in : Result must have length 14, not 16
  56: <Anonymous>
Error : Result must have length 14, not 16

There are 16 rows in the original data frame. The correct filter should give me 14 rows. But what does this error means?

Finally, how do I do the same for a mix of discrete and continuous variables? (say, filtering based on gender, height, etc...)

Thanks

Upvotes: 0

Views: 2345

Answers (1)

dc37
dc37

Reputation: 16178

Without a small reproducible example of your data, it is difficult ot be sure that it is working but did you try:

library(dplyr)
recTable %>% filter(reqStatus == "Open" & reqDate == agendaDate)

If this is not working, please consider to provide a reproducible example of your data as described here: How to make a great R reproducible example

Upvotes: 4

Related Questions