I have a dummy dataset as follows.
my_data <- data.frame(matrix(ncol = 2, nrow = 4))
colnames(my_data) <- c('date', 'value')
my_data[,1] <- as.Date(c("2022-03-08","2023-03-07","2022-11-29","2024-02-22"))
my_data[,2] <- c(1,2,3,4)
I have two vectors of dates as follows:
tox_date_1q <- as.Date(c("2022-03-08","2023-03-07","2024-02-21"))
tox_date_3q <- as.Date(c("2022-06-21","2022-09-14","2022-11-29","2023-06-05","2023-09-13","2023-11-14"))
I want to create a column group
, and categorize each row with one of the following labels.
c("tox_date_1q", "tox_date_3q", "tox_period_1q", "tox_period_3q", "others")
my codes are the following:
my_data <- my_data |>
mutate(group = case_when(
date %in% tox_date_1q ~ "tox_date_1q",
date %in% tox_date_3q ~ "tox_date_3q",
date > as.Date(tox_date_1q-30) & date < as.Date(tox_date_1q+30) ~ "tox_period_1q",
date > as.Date(tox_date_3q-30) & date < as.Date(tox_date_3q+30) ~ "tox_period_3q",
.default = "other"
))
I received a warning message:
Warning message:
There were 4 warnings in `mutate()`.
The first warning was:
ℹ In argument: `group = case_when(...)`.
Caused by warning in `>.default`:
! longer object length is not a multiple of shorter object length
Something wrong with the following codes, but I do not know how to fix it.
date > as.Date(tox_date_1q-30) & date < as.Date(tox_date_1q+30) ~ "tox_period_1q",
date > as.Date(tox_date_3q-30) & date < as.Date(tox_date_3q+30) ~ "tox_period_3q",