Convert column of Unix to date time - r

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

Related

how to load dates in a readable format from excel into r?

I have an excel spreadsheet that has different date formats. R reads in the dates correctly when they're formatted as m/dd/yy, but when they're m/d/yy, it reads them as a code like this: 36251.
To fix it, I found this, which seems useful, but it is only for one date and I have multiple.
How to convert Excel date format to proper date in R
This was what was suggested:
as.Date(42705, origin = "1899-12-30")
So I tried this:
stockindices0$Date <- as.Date(stockindices0$Date , origin = "12-30-99")
but got the error:
Error in charToDate(x) :
character string is not in a standard unambiguous format
I also tried it with the year as 1899 with no success.
You can try this
as.Date(stockindices0$Date, format = "%d/%m/%y")

Converting odd columns to POSIXct format

I am trying to convert the odd columns of a dataframe to a POSIXct format.
My problem is the following one. If I run this:
as.POSIXct(timestamptest2[2,1])
I got the desired format:
"2018-05-01 15:00:16 CEST"
However when I am doing the conversion to all the columns I get this error:
as.POSIXct(timestamptest2[,odd_indexes])
Error in as.POSIXct.default(timestamptest2[, odd_indexes]) :
do not know how to convert 'timestamptest2[, odd_indexes]' to class “POSIXct”
Being odd_indexes a vector containing the colums date have my date in string format.
I have tried as well with:
-apply(timestamptest2[,odd_indexes],2,as.POSIXct)
Error in as.POSIXlt.character(x, tz, ...) :
character string is not in a standard unambiguous format
Anyone knows how to deal with this problem?
This should work:
timestamptest2[odd_indexes] <- lapply(timestamptest2[odd_indexes], as.POSIXct)

From character to posixct

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.

Converting a UNIX epoch to a Date Object

I am trying to convert a supposedly UNIX epoch (obtained from GPS) to date object in R. I tried to use the following code, but I got an unrealistic answer as a result (shown below). Could anybody tell me what type of UNIX this is and how to convert it ?
x <- 2.01604*10^13 # Unix epoch I need to convert
as.POSIXct(as.numeric(as.character(x)), origin="1970-01-01", tz="Asia/Shanghai")
> > "640827-08-24 07:06:40 CST" # result

convert from posix timestamp in %Y-%m-%d in R

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

Resources