如何在 R 中将日期从 d-m-y 重新排列为 m-d-y?
How can I rearrange the date from d-m-y to m-d-y in R?
我在使用以下 R 代码时遇到问题。我正在尝试将列中的 csv 日期值从日-月-年重新排列为月-日-年。出现问题:格式改为年-月-日,当我尝试绘制结果时出现此错误消息:
Error: Column New_Date
is a date/time and must be stored as POSIXct, not POSIXlt.
我是 R 的新手,不确定如何修复此错误。
我经历过很多类似的话题,但是由于对R知识的缺乏,我无法理解这些话题是否可以转化为我自己的代码,以及我需要的信息。
非常感谢任何帮助。该代码相对较快到期,因此任何快速响应都会受到崇拜。谢谢!
structure(list(Date = structure(c(48L, 11L, 36L, 35L, 1L, 14L
), .Label = c("01-02-18", "02-03-18", "02-10-18", "03-01-18",
"03-04-18", "03-05-18", "03-08-18", "03-09-18", "05-07-18", "05-12-18",
"07-02-18", "07-06-18", "07-11-18", "08-03-18", "09-01-18", "09-05-18",
"09-08-18", "09-10-18", "10-01-18", "10-04-18", "10-09-18", "11-07-18",
"12-11-18", "12-12-18", "13-02-18", "13-06-18", "14-03-18", "14-09-18",
"15-01-18", "15-05-18", "16-04-18", "16-08-18", "17-07-18", "18-12-18",
"19-01-18", "19-02-18", "19-06-18", "19-10-18", "19-11-18", "20-03-18",
"20-04-18", "20-08-18", "20-09-18", "21-05-18", "23-07-18", "23-11-18",
"24-12-18", "25-01-18", "25-02-18", "25-05-18", "25-06-18", "25-10-18",
"26-03-18", "26-09-18", "27-04-18", "29-08-18", "30-07-18", "31-05-18",
"31-10-18"), class = "factor"), New_Date = structure(list(sec = c(0,
0, 0, 0, 0, 0), min = c(0L, 0L, 0L, 0L, 0L, 0L), hour = c(0L,
0L, 0L, 0L, 0L, 0L), mday = c(25L, 7L, 19L, 19L, 1L, 8L), mon = c(0L,
1L, 1L, 0L, 1L, 2L), year = c(-1882L, -1882L, -1882L, -1882L,
-1882L, -1882L), wday = c(4L, 3L, 1L, 5L, 4L, 4L), yday = c(24L,
37L, 49L, 18L, 31L, 66L), isdst = c(0L, 0L, 0L, 0L, 0L, 0L),
zone = c("LMT", "LMT", "LMT", "LMT", "LMT", "LMT"), gmtoff = c(NA_integer_,
NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_
)), class = c("POSIXlt", "POSIXt"))), row.names = c(NA, 6L
), class = "data.frame")
编辑:
现在出现此错误:“plot.window(...) 中的错误:需要有限的 'xlim' 值”
下面是我的代码:
beaches$Date = as.Date(as.character(beaches$Date), '%d-%m-%y')
beaches$New_Date = format(beaches$Date, '%m-%d-%y')
Palm_beach = filter(beaches, Site == "Palm Beach")
Shelly_beach = filter(beaches, Site == "Shelly Beach (Manly)")
plot(Palm_beach$Date, Palm_beach$Enterococci..cfu.100ml., col = "green", main = "Palm Beach vs Shelly Beach", xlab = "Dates", ylab = "Enterococci (cfu)")
points(Shelly_beach$Date, Shelly_beach$Enterococci..cfu.100ml., col = "red")
试试这个:
beaches$Date = as.Date(as.character(beaches$Date), '%d-%m-%y')
beaches$New_Date = format(beaches$Date, '%m-%d-%y')
输出:
> head(beaches[, c('Date', 'New_Date')])
Date New_Date
1 2018-01-25 01-25-18
2 2018-02-07 02-07-18
3 2018-02-19 02-19-18
4 2018-01-19 01-19-18
5 2018-02-01 02-01-18
6 2018-03-08 03-08-18
首先您必须确保原始 Date
列是字符格式。
在您的数据中,它是 factor
格式。然后您首先必须将 Date
列转换为日期格式,然后您可以创建 New_Date
列:
df$Date <- as.Date(as.character(df$Date), format = "%d-%m-%y")
df$New_Date <- format(df$Date, "%m-%d-%Y")
如果您只想要年份列的最后两位数字,您可以使用此代替:
df$New_Date2 <- format(df$Date, "%m-%d-%y")
由于输入和输出都不是日期,因此仅使用正则表达式可能更有意义,而不是与日期进行相互转换:
beaches$New_Date <- sub("(\d+)-(\d+)-(\d+)", "\2-\1-\3", beaches$Date)
#### OUTPUT ####
Date New_Date
1 25-01-18 01-25-18
2 07-02-18 02-07-18
3 19-02-18 02-19-18
4 19-01-18 01-19-18
5 01-02-18 02-01-18
6 08-03-18 03-08-18
我在使用以下 R 代码时遇到问题。我正在尝试将列中的 csv 日期值从日-月-年重新排列为月-日-年。出现问题:格式改为年-月-日,当我尝试绘制结果时出现此错误消息:
Error: Column
New_Date
is a date/time and must be stored as POSIXct, not POSIXlt.
我是 R 的新手,不确定如何修复此错误。
我经历过很多类似的话题,但是由于对R知识的缺乏,我无法理解这些话题是否可以转化为我自己的代码,以及我需要的信息。
非常感谢任何帮助。该代码相对较快到期,因此任何快速响应都会受到崇拜。谢谢!
structure(list(Date = structure(c(48L, 11L, 36L, 35L, 1L, 14L
), .Label = c("01-02-18", "02-03-18", "02-10-18", "03-01-18",
"03-04-18", "03-05-18", "03-08-18", "03-09-18", "05-07-18", "05-12-18",
"07-02-18", "07-06-18", "07-11-18", "08-03-18", "09-01-18", "09-05-18",
"09-08-18", "09-10-18", "10-01-18", "10-04-18", "10-09-18", "11-07-18",
"12-11-18", "12-12-18", "13-02-18", "13-06-18", "14-03-18", "14-09-18",
"15-01-18", "15-05-18", "16-04-18", "16-08-18", "17-07-18", "18-12-18",
"19-01-18", "19-02-18", "19-06-18", "19-10-18", "19-11-18", "20-03-18",
"20-04-18", "20-08-18", "20-09-18", "21-05-18", "23-07-18", "23-11-18",
"24-12-18", "25-01-18", "25-02-18", "25-05-18", "25-06-18", "25-10-18",
"26-03-18", "26-09-18", "27-04-18", "29-08-18", "30-07-18", "31-05-18",
"31-10-18"), class = "factor"), New_Date = structure(list(sec = c(0,
0, 0, 0, 0, 0), min = c(0L, 0L, 0L, 0L, 0L, 0L), hour = c(0L,
0L, 0L, 0L, 0L, 0L), mday = c(25L, 7L, 19L, 19L, 1L, 8L), mon = c(0L,
1L, 1L, 0L, 1L, 2L), year = c(-1882L, -1882L, -1882L, -1882L,
-1882L, -1882L), wday = c(4L, 3L, 1L, 5L, 4L, 4L), yday = c(24L,
37L, 49L, 18L, 31L, 66L), isdst = c(0L, 0L, 0L, 0L, 0L, 0L),
zone = c("LMT", "LMT", "LMT", "LMT", "LMT", "LMT"), gmtoff = c(NA_integer_,
NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_
)), class = c("POSIXlt", "POSIXt"))), row.names = c(NA, 6L
), class = "data.frame")
编辑: 现在出现此错误:“plot.window(...) 中的错误:需要有限的 'xlim' 值” 下面是我的代码:
beaches$Date = as.Date(as.character(beaches$Date), '%d-%m-%y')
beaches$New_Date = format(beaches$Date, '%m-%d-%y')
Palm_beach = filter(beaches, Site == "Palm Beach")
Shelly_beach = filter(beaches, Site == "Shelly Beach (Manly)")
plot(Palm_beach$Date, Palm_beach$Enterococci..cfu.100ml., col = "green", main = "Palm Beach vs Shelly Beach", xlab = "Dates", ylab = "Enterococci (cfu)")
points(Shelly_beach$Date, Shelly_beach$Enterococci..cfu.100ml., col = "red")
试试这个:
beaches$Date = as.Date(as.character(beaches$Date), '%d-%m-%y')
beaches$New_Date = format(beaches$Date, '%m-%d-%y')
输出:
> head(beaches[, c('Date', 'New_Date')])
Date New_Date
1 2018-01-25 01-25-18
2 2018-02-07 02-07-18
3 2018-02-19 02-19-18
4 2018-01-19 01-19-18
5 2018-02-01 02-01-18
6 2018-03-08 03-08-18
首先您必须确保原始 Date
列是字符格式。
在您的数据中,它是 factor
格式。然后您首先必须将 Date
列转换为日期格式,然后您可以创建 New_Date
列:
df$Date <- as.Date(as.character(df$Date), format = "%d-%m-%y")
df$New_Date <- format(df$Date, "%m-%d-%Y")
如果您只想要年份列的最后两位数字,您可以使用此代替:
df$New_Date2 <- format(df$Date, "%m-%d-%y")
由于输入和输出都不是日期,因此仅使用正则表达式可能更有意义,而不是与日期进行相互转换:
beaches$New_Date <- sub("(\d+)-(\d+)-(\d+)", "\2-\1-\3", beaches$Date)
#### OUTPUT ####
Date New_Date
1 25-01-18 01-25-18
2 07-02-18 02-07-18
3 19-02-18 02-19-18
4 19-01-18 01-19-18
5 01-02-18 02-01-18
6 08-03-18 03-08-18