如何在R中将日期从d-m-y重新排列为m-d-y?

时间:2019-11-01 08:35:13

标签: r dataframe posixct

我对以下R代码有疑问。我正在尝试将列中的csv日期值从day-month-year-year重新排列为month-day-year-year。出现问题:格式改为年-月-日,并且当我尝试绘制结果时出现此错误消息:

  

错误:列New_Date是日期/时间,必须存储为POSIXct,而不是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")

3 个答案:

答案 0 :(得分:1)

尝试一下:

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

答案 1 :(得分:1)

由于输入和输出都不是日期,因此仅使用常规表达式而不是往返于日期可能更有意义:

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

答案 2 :(得分:0)

首先,您必须确保原始的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")