Format Year-Month to Posixct

format year-month to POSIXct

That's a FAQ: a date is comprised of day, month and year. You are missing one part. So by adding a day, say, '-01', you can impose the missing string structure and parse. Or you can use a more tolerant parser:

R> library(anytime)
R> anydate("2017-06")
[1] "2017-06-01"
R>

Which works for your data too:

R> date
[1] "2016-03" "2016-04" "2016-05" "2016-06"
[5] "2016-07" "2016-08" "2016-09" "2016-10"
[9] "2016-11" "2016-12"
R> anydate(date)
[1] "2016-03-01" "2016-04-01" "2016-05-01"
[4] "2016-06-01" "2016-07-01" "2016-08-01"
[7] "2016-09-01" "2016-10-01" "2016-11-01"
[10] "2016-12-01"
R>

Lastly, your request for POSIXct type is still short an hour, minute and second. But by the same principle:

R> anytime(date)
[1] "2016-03-01 CST" "2016-04-01 CDT"
[3] "2016-05-01 CDT" "2016-06-01 CDT"
[5] "2016-07-01 CDT" "2016-08-01 CDT"
[7] "2016-09-01 CDT" "2016-10-01 CDT"
[9] "2016-11-01 CDT" "2016-12-01 CST"
R>

These two functions return proper Date and POSIXct types, respectively.

Changing month name and year to date or POSIXct

We can use as.yearmon to convert to yearmon class and then change the format

library(zoo)
df1$date <- format(as.yearmon(df1$date, "%B %Y"), "%m-%Y")
df1$date
#[1] "12-2018" "11-2018" "10-2018" "04-2019" "08-2019" "02-2019" "01-2019" "07-2019" "06-2019" "03-2019" "05-2019" "11-2019" "10-2019"
#[14] "09-2019"

data

df1 <- structure(list(date = c("December 2018", "November 2018", "October 2018", 
"April 2019", "August 2019", "February 2019", "January 2019",
"July 2019", "June 2019", "March 2019", "May 2019", "November 2019",
"October 2019", "September 2019"), df_discharge_cfs = c(2520.1394,
3475.1023, 1863.4778, 3244.5356, 335.5074, 1631.3048, 1767.6266,
496.9439, 1097.2101, 1081.8046, 1507.8582, 2842.3542, 544.3002,
295.72), green_discharge_cfs = c(171.69667, 239.00738, 121.9172,
260.38507, 14.95659, 94.35956, 132.69408, 26.37159, 64.17292,
80.32419, 100.81569, 284.72917, 34.67999, 11.37943),
north_discharge_cfs = c(338.81082,
422.19063, 200.94455, 543.34792, 29.29938, 198.19885, 247.54493,
57.50114, 143.40153, 167.57954, 236.58269, 586.75, 83.58193,
26.25823)), class = "data.frame", row.names = c("1", "2", "3",
"4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14"))

Change the date and month only in POSIXct format?

If you want to extract out parts of the date you can use:

dataframe$dateonly <- ymd(strptime(as.character(dataframe$datetime), "%Y-%m-%d"))

you could pull that out or you can also pull out year month and day using lubridate

dataframe$year <- year(dataframe$date) 

and do this for month and day and whatever else and then paste them back together to make a new date if that makes any sense at all?

Convert Date String in Format Mon Day, Year Time am/pm to POSIXlt format in R?

You will want to use the format "%b %d, %Y %I:%M %p" in as.POSIXlt()

  • %b for the abbreviated month (in the current locale)
  • %d for the day
  • %Y for the full four-digit year
  • %I for the hour (when using %p a.k.a am/pm)
  • %M for the minutes
  • %p for am/pm

So for a POSIXlt date-time, you can do

(x <- as.POSIXlt("Aug 19, 2015 07:09 am", format = "%b %d, %Y %I:%M %p"))
# [1] "2015-08-19 07:09:00 PDT"

As mentioned in the comments, you must have created the entire POSIX object in order to extract its parts (well, to formally extract them anyway).

months(x)
# [1] "August"
months(x, abbreviate = TRUE)
# [1] "Aug"

Additionally, in order to use strptime(), you must plan on creating the entire date-time object as you cannot just create a month and have it classed as such. See help(strptime) and help(as.POSIXlt) for more.

How to change year.month format into Year-Month format in R

You can use sub, with capturing groups in the regular expression:

df$Month <- sub("^(\\d{4})\\.(\\d{2})$", "\\1-\\2", format(df$Month, 2))

df
#> Month GSI
#> 1 1993-01 -0.5756706
#> 2 1993-02 -1.1554924
#> 3 1993-03 -1.0035307
#> 4 1993-04 -0.1069888
#> 5 1993-05 -0.3190359
#> 6 1993-06 0.3036164
#> 7 1993-07 1.2452892
#> 8 1993-08 0.8510437
#> 9 1993-09 1.2468009
#> 10 1993-10 1.4252141

Input Data

df <- structure(list(Month = c(1993.01, 1993.02, 1993.03, 1993.04, 
1993.05, 1993.06, 1993.07, 1993.08, 1993.09, 1993.1), GSI = c(-0.57567056,
-1.15549239, -1.00353071, -0.1069888, -0.31903591, 0.30361638,
1.24528915, 0.8510437, 1.24680092, 1.42521406)), class = "data.frame", row.names = c(NA,
-10L))

df
#> Month GSI
#> 1 1993.01 -0.5756706
#> 2 1993.02 -1.1554924
#> 3 1993.03 -1.0035307
#> 4 1993.04 -0.1069888
#> 5 1993.05 -0.3190359
#> 6 1993.06 0.3036164
#> 7 1993.07 1.2452892
#> 8 1993.08 0.8510437
#> 9 1993.09 1.2468009
#> 10 1993.10 1.4252141

how to set the day, month and year in a posixlt in r?

If you have individual strings, then

as.POSIXlt(paste(date, time), format="%d/%m/%Y %H:%M:%S")

should work. Example:

as.POSIXlt(paste("16/12/2006", "17:24:00"), format="%d/%m/%Y %H:%M:%S")
# [1] "2006-12-16 17:24:00"

For your second point, $month and $day aren't available attributes. You can see the attributes available with something like:

dput(as.POSIXlt(Sys.time()))
# structure(list(sec = 48.7993450164795, min = 17L, hour = 0L,
# mday = 18L, mon = 6L, year = 118L, wday = 3L, yday = 198L,
# isdst = 1L, zone = "PDT", gmtoff = -25200L), .Names = c("sec",
# "min", "hour", "mday", "mon", "year", "wday", "yday", "isdst",
# "zone", "gmtoff"), class = c("POSIXlt", "POSIXt"), tzone = c("",
# "PST", "PDT"))

showing that what you really need are $mon and $mday.

Extract month and year from datetime in R

lubridate month and year will work.

as.data.frame(Order.Date) %>%
mutate(Month = lubridate::month(Order.Date, label = FALSE),
Year = lubridate::year(Order.Date))

Order.Date Month Year
1 2011-10-20 10 2011
2 2011-12-25 12 2011
3 2012-04-15 4 2012
4 2012-08-23 8 2012
5 2013-09-25 9 2013

If you want month format as Jan, use month.abb and as January, use month.name

as.data.frame(Order.Date) %>%
mutate(Month = month.abb[lubridate::month(Order.Date, label = TRUE)],
Year = lubridate::year(Order.Date))

Order.Date Month Year
1 2011-10-20 Oct 2011
2 2011-12-25 Dec 2011
3 2012-04-15 Apr 2012
4 2012-08-23 Aug 2012
5 2013-09-25 Sep 2013

as.data.frame(Order.Date) %>%
mutate(Month = month.name[lubridate::month(Order.Date, label = TRUE)],
Year = lubridate::year(Order.Date))

Order.Date Month Year
1 2011-10-20 October 2011
2 2011-12-25 December 2011
3 2012-04-15 April 2012
4 2012-08-23 August 2012
5 2013-09-25 September 2013

Is it possible in R to split my date-time values into 5 different columns (Year, month, date, hour, minute)?

Change it into dataframe then run mutate part will works

x %>% 
as.data.frame() %>%
rename(x = '.') %>%
dplyr::mutate(year = lubridate::year(x),
month = lubridate::month(x),
day = lubridate::day(x),
hour = lubridate::hour(x),
minute = lubridate::minute(x),
second = lubridate::second(x))

x year month day hour minute second
1 2018-01-03 12:34:32 2018 1 3 12 34 32.92382
2 2018-01-03 12:50:40 2018 1 3 12 50 40.00040

How to turn a column in a data frame to as POSIXct class? Only have month and year

You may paste a phantom day.

as.POSIXct(paste0(d$yr_mo, "-01"), tz="GMT")
# [1] "2012-08-01 GMT" "2012-08-01 GMT" "2012-08-01 GMT"

Data:

d <- read.table(text='yr_mo
"2012-08"
"2012-08"
"2012-08"', header=TRUE)


Related Topics



Leave a reply



Submit