Reputation: 331
I have a dataframe containing only dates. I wish to replace all the dates in the whole dataframe that match a date in my vector, not only one column:
holidays
[1] "2022-01-01" "2022-04-15" "2022-04-17" "2022-04-18" "2022-04-27" "2022-05-05" "2022-05-26" "2022-06-05" "2022-06-06" "2022-12-25" "2022-12-26" "2021-04-04"
[13] "2021-04-05" "2021-04-27" "2021-05-05" "2021-05-13" "2021-05-23" "2021-05-24" "2021-12-25" "2021-12-26"
I've only found solutions that delete the whole row if the date in my vector matches the date in my dataframe but I wish to only replace the value with NA, not delete anything.
Further, I would like to replace all the values in the WHOLE dataframe if it is a weekend day. I know how to check this for one column but the whole dataframe I can't seem to manage.
Hope you can help out! Many thanks
>dput(T0range)
structure(list(V1 = structure(c(18708, 18708, 18708, 18708, 18708,
18708, 18709, 18709, 18709, 18709, 18715, 18715, 18715, 18715,
18715), class = "Date"), V2 = structure(c(18709, 18709, 18709,
18709, 18709, 18709, 18710, 18710, 18710, 18710, 18716, 18716,
18716, 18716, 18716), class = "Date"), V3 = structure(c(18710,
18710, 18710, 18710, 18710, 18710, 18711, 18711, 18711, 18711,
18717, 18717, 18717, 18717, 18717), class = "Date"), V4 = structure(c(18711,
18711, 18711, 18711, 18711, 18711, 18712, 18712, 18712, 18712,
18718, 18718, 18718, 18718, 18718), class = "Date"), V5 = structure(c(18712,
18712, 18712, 18712, 18712, 18712, 18713, 18713, 18713, 18713,
18719, 18719, 18719, 18719, 18719), class = "Date"), V6 = structure(c(18713,
18713, 18713, 18713, 18713, 18713, 18714, 18714, 18714, 18714,
18720, 18720, 18720, 18720, 18720), class = "Date"), V7 = structure(c(18714,
18714, 18714, 18714, 18714, 18714, 18715, 18715, 18715, 18715,
18721, 18721, 18721, 18721, 18721), class = "Date"), V8 = structure(c(18715,
18715, 18715, 18715, 18715, 18715, 18716, 18716, 18716, 18716,
18722, 18722, 18722, 18722, 18722), class = "Date"), V9 = structure(c(18716,
18716, 18716, 18716, 18716, 18716, 18717, 18717, 18717, 18717,
18723, 18723, 18723, 18723, 18723), class = "Date"), V10 = structure(c(18717,
18717, 18717, 18717, 18717, 18717, 18718, 18718, 18718, 18718,
18724, 18724, 18724, 18724, 18724), class = "Date"), V11 = structure(c(18718,
18718, 18718, 18718, 18718, 18718, 18719, 18719, 18719, 18719,
18725, 18725, 18725, 18725, 18725), class = "Date"), V12 = structure(c(18719,
18719, 18719, 18719, 18719, 18719, 18720, 18720, 18720, 18720,
18726, 18726, 18726, 18726, 18726), class = "Date"), V13 = structure(c(18720,
18720, 18720, 18720, 18720, 18720, 18721, 18721, 18721, 18721,
18727, 18727, 18727, 18727, 18727), class = "Date"), V14 = structure(c(18721,
18721, 18721, 18721, 18721, 18721, 18722, 18722, 18722, 18722,
18728, 18728, 18728, 18728, 18728), class = "Date"), V15 = structure(c(18722,
18722, 18722, 18722, 18722, 18722, 18723, 18723, 18723, 18723,
18729, 18729, 18729, 18729, 18729), class = "Date")), row.names = c(NA,
-15L), class = "data.frame")
> dput(holidays)
structure(c(18993, 19097, 19099, 19100, 19109, 19117, 19138,
19148, 19149, 19351, 19352, 18721, 18722, 18744, 18752, 18760,
18770, 18771, 18986, 18987), class = "Date")
Upvotes: 0
Views: 498
Reputation: 145775
Unfortunately, I think the easiest way is to lapply
across the columns:
T0range[] = lapply(T0range, function(x){ x[x %in% holidays] <- NA; x})
Upvotes: 2