如果不满足特定条件,则替换 data.table 列中的单个值
Replace a single value in data.table column if it doesn't meet a specific criteria
我有以下 data.table:
> head(sample)
WeekEndingDate Totals_1 Totals_2
1: 2021-06-05 0 0
2: 2021-06-12 0 0
3: 2021-06-19 0 0
4: 2021-06-26 0 0
5: 2021-07-03 0 0
6: 2021-07-10 0 0
> dput(sample)
structure(list(WeekEndingDate = structure(c(18783, 18790, 18797,
18804, 18811, 18818, 18825, 18832, 18839, 18846, 18853, 18860,
18867, 18874, 18881, 18888, 18895, 18902, 18909, 18916, 18923,
18930, 18937, 18944, 18951, 18958, 18965, 18972, 18979, 18986,
18993, 19000, 19007, 19014, 19021, 19028, 19035, 19042, 19049,
19056, 19063, 19070, 19077, 19084, 19091, 19098, 19105, 19112,
19119, 19126, 19133, 19140, 19147, 19154, 19161, 19168, 19175,
19182, 19189, 19196, 19203, 19210, 19217, 19224, 19231, 19238,
19245, 19252, 19259, 19266, 19273, 19280, 19287, 19294, 19301,
19308, 19315, 19322, 19329, 19336, 19343, 19350, 19357, 19364,
19371, 19378, 19385, 19392, 19399, 19406, 19413, 19420, 19427,
19434, 19441, 19448, 19455, 19462, 19469, 19476, 19483, 19490,
19497, 19504, 19511, 19518, 19525), class = "Date"), Totals_1 = c(0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 3, 5, 6, 8,
10, 12, 15, 18, 21, 24, 27, 27, 28, 32, 37, 42, 48, 54, 61, 68,
74, 81, 90, 98, 106, 116, 123, 123, 128, 136, 145, 155, 164,
173, 181, 191, 200, 208, 216, 225, 236, 243, 247, 251, 254, 259,
266, 271, 278, 288, 296, 304, 313, 326, 333, 341, 351, 360, 369,
376, 384, 392, 400, 409, 409, 412, 423, 428, 432, 436, 440, 442,
443, 443, 444, 445, 446, 446, 446, 446, 446, 446, 446, 447, 447,
447, 447, 447, 447), Totals_2 = c(0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 2, 3, 5, 6, 8, 10, 13, 15, 18,
18, 19, 22, 26, 30, 35, 40, 45, 51, 57, 63, 69, 76, 83, 91, 98,
98, 100, 108, 117, 124, 131, 140, 146, 155, 163, 170, 178, 185,
192, 199, 207, 210, 213, 217, 221, 229, 236, 244, 253, 260, 268,
276, 284, 292, 300, 308, 316, 323, 330, 339, 347, 355, 355, 357,
364, 372, 381, 389, 398, 407, 413, 418, 422, 424, 426, 427, 427,
428, 429, 429, 429, 429, 429, 429, 429, 429, 429)), row.names = c(NA,
-107L), class = c("data.table", "data.frame"))
在同一环境中,我定义了一个具有特定日期的变量 x_date
。对于此示例,该值为 2023-09-30
。现在我已经能够使用以下语句识别具有最大值的行:
> class(x_date)
[1] "Date"
> x_date
[1] "2023-09-30"
> sample[sample[,.I[which.max(WeekEndingDate)]]]
WeekEndingDate Totals_1 Totals_2
1: 2023-06-17 447 429
但是,我想要做的是如果最大值 WeekEndingDate
在 x_date
之前
比我想将该值更新为 x_date
否则最大值就可以了。我在想这样的事情会起作用,但这会替换列中的所有值而不仅仅是最大值:
max(sample$WeekEndingDate) <- ifelse(max(sample$WeekEndingDate) < x_date, x_date, max(sample$WeekEndingDate))
library(data.table)
x_date <- as.Date('2023-09-30')
sample[WeekEndingDate==max(WeekEndingDate),WeekEndingDate:=ifelse(WeekEndingDate<x_date,x_date,WeekEndingDate)]
我有以下 data.table:
> head(sample)
WeekEndingDate Totals_1 Totals_2
1: 2021-06-05 0 0
2: 2021-06-12 0 0
3: 2021-06-19 0 0
4: 2021-06-26 0 0
5: 2021-07-03 0 0
6: 2021-07-10 0 0
> dput(sample)
structure(list(WeekEndingDate = structure(c(18783, 18790, 18797,
18804, 18811, 18818, 18825, 18832, 18839, 18846, 18853, 18860,
18867, 18874, 18881, 18888, 18895, 18902, 18909, 18916, 18923,
18930, 18937, 18944, 18951, 18958, 18965, 18972, 18979, 18986,
18993, 19000, 19007, 19014, 19021, 19028, 19035, 19042, 19049,
19056, 19063, 19070, 19077, 19084, 19091, 19098, 19105, 19112,
19119, 19126, 19133, 19140, 19147, 19154, 19161, 19168, 19175,
19182, 19189, 19196, 19203, 19210, 19217, 19224, 19231, 19238,
19245, 19252, 19259, 19266, 19273, 19280, 19287, 19294, 19301,
19308, 19315, 19322, 19329, 19336, 19343, 19350, 19357, 19364,
19371, 19378, 19385, 19392, 19399, 19406, 19413, 19420, 19427,
19434, 19441, 19448, 19455, 19462, 19469, 19476, 19483, 19490,
19497, 19504, 19511, 19518, 19525), class = "Date"), Totals_1 = c(0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 3, 5, 6, 8,
10, 12, 15, 18, 21, 24, 27, 27, 28, 32, 37, 42, 48, 54, 61, 68,
74, 81, 90, 98, 106, 116, 123, 123, 128, 136, 145, 155, 164,
173, 181, 191, 200, 208, 216, 225, 236, 243, 247, 251, 254, 259,
266, 271, 278, 288, 296, 304, 313, 326, 333, 341, 351, 360, 369,
376, 384, 392, 400, 409, 409, 412, 423, 428, 432, 436, 440, 442,
443, 443, 444, 445, 446, 446, 446, 446, 446, 446, 446, 447, 447,
447, 447, 447, 447), Totals_2 = c(0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 2, 3, 5, 6, 8, 10, 13, 15, 18,
18, 19, 22, 26, 30, 35, 40, 45, 51, 57, 63, 69, 76, 83, 91, 98,
98, 100, 108, 117, 124, 131, 140, 146, 155, 163, 170, 178, 185,
192, 199, 207, 210, 213, 217, 221, 229, 236, 244, 253, 260, 268,
276, 284, 292, 300, 308, 316, 323, 330, 339, 347, 355, 355, 357,
364, 372, 381, 389, 398, 407, 413, 418, 422, 424, 426, 427, 427,
428, 429, 429, 429, 429, 429, 429, 429, 429, 429)), row.names = c(NA,
-107L), class = c("data.table", "data.frame"))
在同一环境中,我定义了一个具有特定日期的变量 x_date
。对于此示例,该值为 2023-09-30
。现在我已经能够使用以下语句识别具有最大值的行:
> class(x_date)
[1] "Date"
> x_date
[1] "2023-09-30"
> sample[sample[,.I[which.max(WeekEndingDate)]]]
WeekEndingDate Totals_1 Totals_2
1: 2023-06-17 447 429
但是,我想要做的是如果最大值 WeekEndingDate
在 x_date
之前
比我想将该值更新为 x_date
否则最大值就可以了。我在想这样的事情会起作用,但这会替换列中的所有值而不仅仅是最大值:
max(sample$WeekEndingDate) <- ifelse(max(sample$WeekEndingDate) < x_date, x_date, max(sample$WeekEndingDate))
library(data.table)
x_date <- as.Date('2023-09-30')
sample[WeekEndingDate==max(WeekEndingDate),WeekEndingDate:=ifelse(WeekEndingDate<x_date,x_date,WeekEndingDate)]