I have this character type vector that consists of a year and a month. I want to convert it to a date type, but when I try to do this with the POSIXct function, I get the error:
Error in as.POSIXlt.character(x, tz, ...) :
character string is not in a standard unambiguous format
I can't seem to figure out why it won't work. Anyone?
old <- as.character("201702")
library(lubridate)
new <- as.POSIXct(date, origin = "201501")
You just need to convert to a date first with the appropriate formatting:
as.POSIXct(as.Date("201702", format = "%Y%m"))
You could also assume the you want the 1st of the month?
as.POSIXct(as.Date(paste0("201702", "01"), format = "%Y%m%d"))
Be careful with POSIX dates because they contain the time as well and then timezone that you specify can therefore also play a role in the date you get.
Related
Following is the error I am getting when trying to convert the date column read from csv
data<-read.csv("data.csv",stringsasfactors=FALSE) #data<-read.table("data.csv",sep=",",header=TRUE)
as.Date(data$Date,"%Y-%d-%m")
Error:
error in as.date.default(x ...) do not know how to convert 'x' to class date
If your dates look like 30-06-2003, then you need to adjust the format in which you want to read them accordingly. With this, I mean that the right format is "%d-%m-%Y", i.e. day - month - 4 digit year, and not "%Y-%d-%m".
Try this line
as.Date(data$Date, format = "%d-%m-%Y")
Trying to convert a full column of Unix into date time in R. I've been able to use
as.POSIXlt(x, origin = "1970-01-01")
to convert Unix. But now I'm trying to convert a whole column of Unix into date time using:
as.POSIXct(x$time, origin="1970-01-01")
Which gives me:
Error in as.POSIXlt.character(as.character(x), ...) :
character string is not in a standard unambiguous format`
What is the best way to be doing this?
as.POSIXct(as.numeric(x$time), origin="1970-01-01")
I wish to import my csv file into a data frame but the date in my csv file is in a non-standard format.
The date in the first column is in the following format:
08.09.2016
One of the arguments in my read.csv2 functions is to specify the classes and when I specify this column as a date I receive the following error upon execution:
Error in charToDate(x) :
character string is not in a standard unambiguous format
I'm guessing it doesn't like converting the date from factor class to date class.
I've read a little about POSIXlt but I don't understand the details of the function.
Any ideas how to convert the class from factor to date??
When you convert character to date, you need specify format if it is not standard. The error you got is the result of as.Date("08.09.2016"). But if you do as.Date("08.09.2016", format = "%m.%d.%Y"), it is fine.
I am not sure whether it is possible to pass format to read.csv2 for correct date formatting (maybe not). I would simply read in this date column as factor, then do as.Date(as.character(), format = "%m.%d.%Y") on this column myself.
Generally we use the following format "dd/mm/yy" how can I reorganise the date to that format?
Use format(, format = "%d/%m/%y").
A complete example:
format(as.Date("08.09.2016", format = "%m.%d.%Y"), format = "%d/%m/%y")
# [1] "09/08/16"
I have a data set that has a column for dates (emp2$hiredate) which is in char format. While converting it to Date, I'm getting the following error :
> date1 <- emp2$hiredate[2]
> str(date1)
chr "20FEB1981"
> as.POSIXct(date1)
Error in as.POSIXlt.character(x, tz, ...) :
character string is not in a standard unambiguous format
How do I solve this problem?
R doesn't automatically know the format of the date in the string. You need to use the format argument to tell it how to process the string.
You can use as.Date to create a date
as.Date("20FEB1981", "%d%b%Y")
# [1] "1981-02-20"
Or use as.POSIXct with the format option to create a date-time
as.POSIXct("20FEB1981", format = "%d%b%Y")
# [1] "1981-02-20 PST"
%d means the day of the month in decimal form
%b means that the month is not in numeric form, but rather an abbreviated name
%Y (capitalized) means the year is in century form (all four numbers)
i have date in this format:
1397758632
and i need to convert them into dates in this format %Y-%m-%d.
how can i do this? my timezone is GTM.
Using as.POSIXct() function in this way:
as.POSIXct(x="1397758632",origin="1960-01-01", tz="GMT")
i have this error
Error in as.POSIXlt.character(x, tz, ...) : character string is not in a standard unambiguous format
how can i obtain a readable data?
A couple of things, if you have defined a variable x = "1397758632" you don't have to declare it again, just use x. eg// as.POSIXct(x,origin="1970-01-01", tz="GMT")
2nd, x="1397758632" is actually a char variable, try str(x) you would want to declare it as numeric, x=1397758632 and you should be all good.
Lastly, add as.Date() if you just want the date and not the whole time stamp.
So something like:
x=1397758632
as.POSIXct(x,origin="1970-01-01", tz="GMT")
as.Date(as.POSIXct(x,origin="1970-01-01", tz="GMT")) # for just the date