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
Related
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.
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 am not too great with manipulating dates in R, and am currently stuck on trying to convert a date that has an ambiguous format into a Date object. My dates are in the format 20150122, for example. When I use as.Date, I get an error like:
d7$Search.Date <- as.Date(d7$Search.Date, "%y%m%d")
Error in charToDate(x) :
character string is not in a standard unambiguous format
How can I convert these currently? Thank you.
We need to use %Y as the year part is 4 digits, %y is used when it is 2 digit.
as.Date(d7$Search.Date, "%Y%m%d")
For example
as.Date("20150122", "%Y%m%d")
#[1] "2015-01-22"
For more info regarding the format, check ?strptime
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)
My data, DATA, has a variable, TIME, for which the values print out in this format: "11/14/2006 20:10". For TIME, its mode is numeric and its class is a factor.
I need to convert TIME to a proper date/time variable (DTIME) and add the new DTIME to DATA as date.time. I was told I may have to coerce the time values so that they follow the h:m:s format...Think character string manipulation. Below is my code:
library("chron")
VAR=c(as.character(DATA$TIME))
DT<-t(as.data.frame(strsplit(VAR," ")))
DT[1:3,]
row.names(DT)<-NULL
DT[1:3,]
DTIME<-chron(dates=DT[,1],times=DT[,2],
format=c("m/d/y","h:m"))
But once I run the last line of code, I get the following error message:
Error in convert.times(times., format = format[[2]]) :
format h:m may be incorrect
In addition: Warning message:
In is.na(out$s) : is.na() applied to non-(list or vector) of type 'NULL'
I don't understand what this means, much less how to fix it.
Its not clear from the question exactly what you have -- in such cases its best to show the output of dput applied to your variable -- but assuming you can convert it to character format using as.character or format then its just a matter of using as.chron :
> library(chron)
> TIME <- "11/14/2006 20:10"
> as.chron(TIME, "%m/%d/%Y %H:%M")
[1] (11/14/06 20:10:00)
Use lubridate - it's a fantastic library that will save you much effort.
library(lubridate)
x <- "11/14/2006 20:10"
> mdy_hm(x)
[1] "2006-11-14 20:10:00 UTC"
All lubridate's time conversion function follow a very similar pattern: e.g. "2013-04-01" can be parsed with ymd, etc.
You can use as.POSIXct to convert string into time.
TIME <- "11/14/2006 20:10"
as.POSIXct(TIME, format="%m/%d/%Y %H:%M", tz='GMT')
## [1] "2006-11-14 20:10:00 GMT"