I am trying to covert the following date formats. I have run into trouble using parse_date_time in lubridate package and the strfttime function since it either converts the entire column to the same date or because it keeps on returning the day value. I don't want to see any dates in my solution.
mydata=data.frame(dates=c(200102,200102,200111,200202),desired=c('2001-02','2001-02', '2001-11','2002-02'))
I want to return just the YYYY-mm format in my column. I am having trouble doing this. I have tried using
Try this:
transform(mydata, desired = sub("(....)(..)", "\\1-\\2", dates))
This form is not that convenient for manipulation (plotting, etc.). You might prefer to use the "yearmon" class from the zoo package which stores internally the year/months as year + fraction where fraction is 0 for Jan, 1/12 for Feb, ..., 11/12 for Dec. On output the default rendering is, for example, Jan 2000:
library(zoo)
transform(mydata, ym = as.yearmon(as.character(dates), "%Y%m"))
Related
I have year, month, and day as separate numeric values and would like to create a date object. In most languages like C# or javascript I can do something like
let x=new Date(2010,11,19);
to initialize a date. What's the R equivalent of this? Obviously I can do
as.Date(paste0(year,'-',month,'-',day))
But it seems pointlessly dumb to convert to character in order to convert to date, right? Surely there is a way to cut out the unnecessary extra string conversion?
We can use ISOdate which returns datetime object (POSIXct) which can changed to Date class with as.Date
as.Date(ISOdate(year, month, day))
#[1] "2010-11-19"
data
year <- 2010
month <- 11
day <- 19
I'm trying to convert a column of full integers into date format of abbreviated months. The column has numbers like : 01 02 04 15 13. etc. I want these numbers to show the month they correspond to. Could someone please tell me how. the code I'm trying is this:
#Changing integers to Month Abbrev.
dets_per_month$monthcollected = as.POSIXlt(dets_per_month$monthcollected, format = "%m", origin = "%m")
but I realize the column doesn't have an origin because it's not in date format.
month.abb[as.integer(dets_per_month$monthcollected)]
I would recommend the lubridate package for all things date-time related. It's a nifty package and has more utility than base R, but YMMV.
library(lubridate)
x <- rep(1:12, 2)
lubridate::month(x, label=TRUE)
How can set R to count months instead of dates when converting integers to dates?
After reading several threads on how to convert dates in R, it seems like nobody has asked how it is possible to convert numeric dates if the numerics is given in monthly timeseries. E.g. 552 represents January 2006.
I have tried several things, such as using as.Date(dates,origin="1899-12-01"), but I reckognize that R counts days instead of months. Thus, the code on year-month number 552 above yields "1901-06-06" instead of the correct 2006-01-01.
Sidenote: I also want the format to be YEARmonth, but does R allow displaying dates without days?
I think your starting date should be '1960-01-01'.
anyway you can solve this problem using the package lubridate.
in this case you can start from a date and add months.
library(lubridate)
as.Date('1960-01-01') %m+% months(552)
it gives you
[1] "2006-01-01"
you can display only the year and month of a date, but in that case R coerces the date into a character.
format(as.Date('2006-01-01'), "%Y-%m")
I have a column of my dataframe as
date
17-Feb
17-Mar
16-Dec
16-Nov
16-Sep
17-Feb
I am trying to convert it into a date column from string. I am using the following pieces of code:
as.Date(df$Date, format="%y-%b")
and
as.POSIXct(df$Date, format="%y-%b")
Both of them give NAs
I am getting the format from this link
The starting number is year. Sorry for the confusion.
I assume from your approach that the 17 and 16 refer to the year 2017 and 2016 respectively. You need to also specify the day of month. If you don't care about it, then set it to the 1st.
A slight modification to your code will work, by appending '-01' to the date then updating your format argument to reflect this:
df = data.frame(Date = c("17-Feb", "17-Mar", "16-Dec"))
as.Date(paste0(df$Date, "-01"), format="%y-%b-%d")
I have log files where the date is mentioned in the ordinal date format.
wikipedia page for ordinal date
i.e 14273 implies 273'rd day of 2014 so 14273 is 30-Sep-2014.
is there a function in R to convert ordinal date (14273) to (30-Sep-2014).
Tried the date package but didn come across a function that would do this.
Try as.Date with the indicated format:
as.Date(sprintf("%05d", 14273), format = "%y%j")
## [1] "2014-09-30"
Notes
For more information see ?strptime [link]
The 273 part is sometimes referred to as the day of the year (as opposed to the day of the month) or the day number or the julian day relative to the beginning of the year.
If the input were a character string of the form yyjjj (rather than numeric) then as.Date(x, format = "%y%j") will do.
Update Have updated to also handle years with one digit as per comments.
Data example
x<-as.character(c("14273", "09001", "07031", "01033"))
Data conversion
x1<-substr(x, start=0, stop=2)
x2<-substr(x, start=3, stop=5)
x3<-format(strptime(x2, format="%j"), format="%m-%d")
date<-as.Date(paste(x3, x1, sep="-"), format="%m-%d-%y")
You can use lubridate package as follows:
>library(lubridate)
# Create a template date object
>date <- as.POSIXlt("2009-02-10")
# Update the date using
> update(date, year=2014, yday=273)
[1] "2014-09-30 JST"