0

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

Tristan Tran
  • 1,351
  • 1
  • 10
  • 36
  • 3
    Don't use `recTable$` within dplyr verb functions, just the column names directly. It might be beneficial to go through the tutorials and vignettes at https://dplyr.tidyverse.org/. – r2evans Dec 27 '19 at 21:47
  • 1
    [See here](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) on making an R question that folks can help with. That includes a sample of data and all necessary code. – camille Dec 27 '19 at 21:59
  • Thanks for the source. I will check it out. – Tristan Tran Dec 27 '19 at 22:01

1 Answers1

4

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

dc37
  • 15,840
  • 4
  • 15
  • 32
  • 1
    I'm often torn between the R-idiomatic logic of `&` and the `dplyr`-idiomatic way of implicit "and" by way of `filter(reqStatus == "Open", reqDate == agendaDate)`. They both work, of course, and there's no direct `dplyr`-idiomatic way of "or", so perhaps the explicit use of `&` provides a consistent and declarative manner. \*shrug\* – r2evans Dec 27 '19 at 21:50
  • Actually, I didn't know that `filter(reqStatus == "Open", reqDate == agendaDate)` was working too ;) thanks for pointing that. I like the `&` because it is really easy to understand (and oppositely, I hate the `%in%` I can't get to remember it even if it is super-useful to know it) – dc37 Dec 27 '19 at 21:53
  • `%in%` is not needed here, though it would also work. As for comma-separated, it's used in many of the examples on https://dplyr.tidyverse.org/reference/filter.html (and in `?filter`). – r2evans Dec 27 '19 at 21:56
  • 1
    Good to know ! (I was not mentioning %in% for the current response but as an example of the kind of symbols that I found strange to use. But you're right, it's out of focus) – dc37 Dec 27 '19 at 21:57
  • Thanks. This works. Also, I found what is wrong with my code. In the second filter, I should use ```openTable$reqDate``` instead of the original table. But your solution is much better. – Tristan Tran Dec 27 '19 at 22:02
  • Glad that it helps you ;) – dc37 Dec 27 '19 at 22:05