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