如何过滤日期的一部分,然后更改该部分



test_2中有一组名为df的日期,我正在尝试更改。例如:2020-12-15started_at列中,2020-12-25ended_at列中。我想更改ended_at列的日期部分。

我可以写day(test_2$ended_at) <- 15#[感谢Ben用这个区块指导我]

但问题是,还有其他一些日子。如2020-12-08

如何筛选日期中所需的部分并进行更改?我衷心感谢你的帮助。

这是数据结构的dput。

> dput(test_2)  
structure(list(started_at = structure(c(1608033433, 1608033092, 
1608033242, 1608034138, 1608034548, 1608033904, 1608033525, 1608032413, 
1608032432, 1607385918, 1608032241, 1608034867, 1609079592, 1608033139, 
1608032406, 1608034912, 1608033844, 1608032114, 1608034239, 1608032677, 
1608032219, 1608033975, 1609459101, 1608032929, 1608034558, 1608034138, 
1608033654, 1608033875, 1606810523, 1608034878, 1608034232), tzone = "UTC", class = c("POSIXct", 
"POSIXt")), ended_at = structure(c(1608914839, 1608908027, 1608909124, 
1608924913, 1608905112, 1608920814, 1608915081, 1608891612, 1608896054, 
1607385667, 1608891462, 1608922015, 1606985651, 1608907113, 1608896350, 
1608923619, 1608923486, 1608887393, 1608934063, 1608899164, 1608886816, 
1608924042, 1606781193, 1608907025, 1608914882, 1608923510, 1608921699, 
1608922845, 1606810492, 1608913874, 1608943331), tzone = "UTC", class = c("POSIXct", 
"POSIXt"))), row.names = c(NA, -31L), class = c("tbl_df", "tbl", 
"data.frame"))

根据描述,我们可以创建一个逻辑索引,用于子设置和更新"ended_at"列

library(lubridate)
i1 <- with(test_2, as.Date(started_at) == "2020-12-15" & 
        as.Date(ended_at ) == "2020-12-25")
day(test_2$ended_at[i1]) <- 15

相关内容

最新更新