How can I rearrange the date from d-m-y to m-d-y in R?

情到浓时终转凉″ 提交于 2020-03-03 09:21:31

问题


I am having issues with the following R code. I am trying to rearrange csv date values in a column from day-month-year to month-day-year. To issues arise: the format is changed to year-month-day instead, and this error message appears when I attempt to plot the results:

Error: Column New_Date is a date/time and must be stored as POSIXct, not POSIXlt.

I am new to R and unsure on how to fix this error.

I have gone through a lot of similar topics, however because of lack of knowledge in R, I am unable to understand whether these topics can translate to my own code, and the information that I need.

Any help is much appreciated. The code is due relatively soon, so any fast responses are going to be worshipped. Thanks!

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")

EDIT: Now having this error appear: "'Error in plot.window(...) : need finite 'xlim' values" Below is my code:

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")

回答1:


Try this:

beaches$Date = as.Date(as.character(beaches$Date), '%d-%m-%y')
beaches$New_Date = format(beaches$Date, '%m-%d-%y')

Output:

> 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



回答2:


Since neither input nor output are dates it might make more sense to just use regular expresions, rather than converting to and from dates:

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



回答3:


first of all you have to make sure that the original Date column is in character format. In your data it is in factor format. Then you first have to convert the Date column to a date format and then you can create the New_Date column:

df$Date <- as.Date(as.character(df$Date), format = "%d-%m-%y")
df$New_Date <- format(df$Date, "%m-%d-%Y")

If you only want the last two digits of the year column you can use this instead:

df$New_Date2 <- format(df$Date, "%m-%d-%y")


来源:https://stackoverflow.com/questions/58656245/how-can-i-rearrange-the-date-from-d-m-y-to-m-d-y-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!