Hello Team, I want to filter data set base on date, since this data will be continue , I do not want to keep edit the date line.
last_one_days_cases<-Conj_daily_case%>%
filter(max(date, na.rm = TRUE))%>%
group_by(district)%>%
summarise(cases=sum(cases))
the following is the error that is show up
Error in filter()
: In argument: max(date, na.rm = TRUE)
. Caused by error: ! ..1
must be a logical vector, not a object. Backtrace: 1. ... %>% summarise(cases = sum(cases)) 11. dplyr:::dplyr_internal_error(...)
[image] Show Traceback
Error in filter(., max(date, na.rm = TRUE)) : Caused by error: ! ..1
must be a logical vector, not a object.
however I could use the following code
last_one_days_cases<-Conj_daily_case%>%
filter(date>="2024-01-27")%>%
group_by(district)%>%
summarise(cases=sum(cases))
but I do not want to keep on editing the date every time i want to run this code.
the data is in this format
|date|district|cases|
|2024-01-25|a|2|
|2024-01-25|a|5|
|2024-01-25|b|7|
|2024-01-26|c|4|
|2024-01-27|a|3|
|2024-01-28|b|7|
|2024-01-28|c|8|
|2024-01-28|b|9|
|2024-01-28|f|3|
|2024-01-28|f|1|
|2024-01-29|a|3|
|2024-01-29|g|1|
|2024-01-29|a|1|
is there a way of doing it better.
FJCC
March 14, 2024, 9:33pm
2
As the error mentions, the filter() function expects logical values, TRUE or FALSE, to use in filtering. You want to keep dates that equal the maximum date, so use code like this
Conj_daily_case <- read.csv("~/R/Play/Dummy.csv", sep = "|")
library(tidyverse)
last_one_days_cases <- Conj_daily_case %>%
filter(date == max(date, na.rm = TRUE)) %>%
group_by(district) %>%
summarise(cases=sum(cases))
last_one_days_cases
#> # A tibble: 2 × 2
#> district cases
#> <chr> <int>
#> 1 a 4
#> 2 g 1
Created on 2024-03-14 with reprex v2.0.2
I would also convert your date column into numeric dates instead of finding the max() of character values.
library(tidyverse)
last_one_days_cases <- Conj_daily_case %>%
mutate(date = ymd(date)) %>%
filter(date == max(date, na.rm = TRUE)) %>%
group_by(district) %>%
summarise(cases=sum(cases))
last_one_days_cases
thanks a lot it has worked.
I have a follow up question how best can one calculate the last 7 days using that method ?
FJCC
March 14, 2024, 10:12pm
4
This will return data from the the last seven days.
last_one_days_cases <- Conj_daily_case %>%
mutate(date = ymd(date)) %>%
filter(max(date) - date < 7, na.rm = TRUE)
system
Closed
March 21, 2024, 10:31pm
6
This topic was automatically closed 7 days after the last reply. New replies are no longer allowed. If you have a query related to it or one of the replies, start a new topic and refer back with a link.