如何根据其索引间隔更改日期Posixct列的年份?

发布于 2025-02-13 07:01:34 字数 1369 浏览 0 评论 0原文

我的数据看起来像这样:

> dput(head(nAUR,50))
structure(list(date = structure(c(1483440272, 1484304241, 1484564041, 
1484823831, 1485428001, 1485687791, 1486032161, 1486291941, 1486551731, 
1486896101, 1487155881, 1487415671, 1487760031, 1488019821, 1488279601, 
1488624021, 1488883821, 1489143521, 1489488011, 1489747821, 1490007451, 
1490352021, 1490608221, 1490867781, 1491212421, 1491472221, 1491731811, 
1492076421, 1492336231, 1492595781, 1492940421, 1493200231, 1493459811, 
1493804421, 1494064231, 1494323781, 1494668431, 1494928231, 1495187811, 
1495532431, 1495792231, 1496051781, 1496396421, 1496656231, 1496915811, 
1497260421, 1497520231, 1497779781, 1498124421, 1498384231), tzone = "", class = c("POSIXct", 
"POSIXt")), layer = c("AUR", "AUR", "AUR", "AUR", "AUR", "AUR", 
"AUR", "AUR", "AUR", "AUR", "AUR", "AUR", "AUR", "AUR", "AUR", 
"AUR", "AUR", "AUR", "AUR", "AUR", "AUR", "AUR", "AUR", "AUR", 
"AUR", "AUR", "AUR", "AUR", "AUR", "AUR", "AUR", "AUR", "AUR", 
"AUR", "AUR", "AUR", "AUR", "AUR", "AUR", "AUR", "AUR", "AUR", 
"AUR", "AUR", "AUR", "AUR", "AUR", "AUR", "AUR", "AUR")), row.names = c(NA, 
-50L), class = c("tbl_df", "tbl", "data.frame"))

我想更改2017-01-13之间的日期 10:44:01(1484304241),直到2017-03-17 10:50:21(14897477821)到 2016 。我必须使用数据框中的位置间隔(索引)进行此操作,因为在我的原始数据集中,我要更改的那些日期都重复了。

任何帮助将不胜感激。

My data looks like this:

> dput(head(nAUR,50))
structure(list(date = structure(c(1483440272, 1484304241, 1484564041, 
1484823831, 1485428001, 1485687791, 1486032161, 1486291941, 1486551731, 
1486896101, 1487155881, 1487415671, 1487760031, 1488019821, 1488279601, 
1488624021, 1488883821, 1489143521, 1489488011, 1489747821, 1490007451, 
1490352021, 1490608221, 1490867781, 1491212421, 1491472221, 1491731811, 
1492076421, 1492336231, 1492595781, 1492940421, 1493200231, 1493459811, 
1493804421, 1494064231, 1494323781, 1494668431, 1494928231, 1495187811, 
1495532431, 1495792231, 1496051781, 1496396421, 1496656231, 1496915811, 
1497260421, 1497520231, 1497779781, 1498124421, 1498384231), tzone = "", class = c("POSIXct", 
"POSIXt")), layer = c("AUR", "AUR", "AUR", "AUR", "AUR", "AUR", 
"AUR", "AUR", "AUR", "AUR", "AUR", "AUR", "AUR", "AUR", "AUR", 
"AUR", "AUR", "AUR", "AUR", "AUR", "AUR", "AUR", "AUR", "AUR", 
"AUR", "AUR", "AUR", "AUR", "AUR", "AUR", "AUR", "AUR", "AUR", 
"AUR", "AUR", "AUR", "AUR", "AUR", "AUR", "AUR", "AUR", "AUR", 
"AUR", "AUR", "AUR", "AUR", "AUR", "AUR", "AUR", "AUR")), row.names = c(NA, 
-50L), class = c("tbl_df", "tbl", "data.frame"))

I want to change the YEAR of the dates between 2017-01-13 10:44:01 (1484304241) until 2017-03-17 10:50:21 (1489747821) to 2016. I have to do it using their position interval in the dataframe (index) because in my original data set those dates I want to change are repeated.

Any help will be much appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

土豪 2025-02-20 07:01:34

您可以使用`年< - `()() lubridate 替换DateTime对象的年份。

library(dplyr)
library(lubridate)

nAUR %>%
  mutate(date2 = if_else(between(date, 1484304241, 1489747821),
                         `year<-`(date, 2016), date))

更新
nAUR %>%
  mutate(date2 = if_else(between(row_number(), which(date == 1484304241)[1], which(date == 1489747821)[1]),
                         `year<-`(date, 2016), date))

You could use `year<-`() from lubridate to replace the year of a datetime object.

library(dplyr)
library(lubridate)

nAUR %>%
  mutate(date2 = if_else(between(date, 1484304241, 1489747821),
                         `year<-`(date, 2016), date))

Update
nAUR %>%
  mutate(date2 = if_else(between(row_number(), which(date == 1484304241)[1], which(date == 1489747821)[1]),
                         `year<-`(date, 2016), date))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文