i'm fairly new to R
i have these values:
> results$batterydate[file_i]
[1] "2014-05-20 EDT"
> L[[13]]
[1] 1400558400
I'm trying to do:
results$batterydate[file_i] <- L[[13]]
But end up with this error:
Error in as.POSIXct.numeric(value) : 'origin' must be supplied
I'm just trying to replace the value in results$batterydate[file_i] with L[[13]], what's going on?
try,
results$batterydate[file_i] <- as.POSIXct(L[[13]],origin="1970-01-01")
1400558400 is 2014-05-20 06:00:00 CEST in POSIXct, i.e.,
as.POSIXct(1400558400,origin="1970-01-01")
you can use unclass() to extract the raw value, if that's what you are looking to do,
(z <- Sys.time())
#> [1] "2018-02-02 21:45:47 CET"
unclass(z)
#> [1] 1517604347
if you can provide a complete minimal reproducible example to go along with your question we can work from it would be possible to show specifically how it might be possible to solve your issue.
Related
My input data, formatted as character, looks like this
"2020-07-10T00:00:00"
I tried
library(lubridate)
mdy_hms("2020-07-10T00:00:00", format='%Y-%m-%dT%H:%M:%S', tz=Sys.timezone())
But I get
[1] NA NA
Warning message:
All formats failed to parse. No formats found.
I tried the more flexibel approach parse_date_time(), but without luck
parse_date_time("2020-07-10T00:00:00", '%Y-%m-%dT%H:%M:%S', tz=Sys.timezone())
How can I convert this date "2020-07-10T00:00:00" to a date R recognizes? Note: I am not interested in the time really, only the date!
Why not just
as.Date("2020-07-10T00:00:00")
# [1] "2020-07-10"
Fun fact:
as.Date("2020-07-101sddT00:1sdafsdfsdf0:00sdfzsdfsdfsdf")
# [1] "2020-07-10"
Assuming that the 07 is the month of July, and the 10 is the 10th:
x <- "2020-07-10T00:00:00"
ymd_hms(x, tz = Sys.timezone())
> [1] "2020-07-10 AEST"
If it's in format year-day-month, swap the ymd for ydm.
Hope this helps!
Is there a way to set default origin for as.Date?
I did this function to workaround:
as.date=function(x, origin='1970-01-01') as.Date(x, origin=origin)
For example:
as.Date(0)
Error in as.Date.numeric(0) : 'origin' deve ser especificado
as.date(0)
[1] "1970-01-01"
The zoo package adds a default origin:
library(zoo)
as.Date(0)
## [1] "1970-01-01"
Update
This is several years later but it looks like R has added .Date so we can now do this using only base R.
.Date(0)
## [1] "1970-01-01"
There is an elegant and simple solution, like zoo, but allows for some tweaking if you need it:
require(anytime)
The base is simply:
anytime(0)
which returns for me in eastern standard time:[1] "1969-12-31 19:00:00 EST"
If you want to be able to force it to the UTC center of the temporal universe
anytime(0, asUTC=TRUE)
which returns
[1] "1970-01-01 UTC"
And if you want to tell R that you your data is from a given time zone :
Sys.setenv(TZ= 'desiredTimeZone') with anytime:::getTZ() as your desired time zone if that is the one, in which, your dates were gathered.
Any of the answers will work, this one just gives you control over the integer (or string) of numerals as well as the time zone...so it is pretty universally useful if you are working with data gathered remotely.
The lubridate package has been specically made the work with dates easier :
library(lubridate)
as_date(0)
#[1] "1970-01-01"
Not really. There is no way the origin date can be changed and remain applicable forever in a session.
If you look at parameters for as.Date (i.e. function then origin does not has a default value when x is in numeric.
## S3 method for class 'numeric'
as.Date(x, origin, ...)
Perhaps, it would have been a good extension to as.Date function to provide default value for origin.
OP has done write thing to create a wrapper function to remove dependency on origin. Perhaps the function can be improved slightly like:
Modified function based on suggestions from suggestions from #sm1 and #Gregor.
## if date.origin is not defined then origin will be taken as "1970-01-01
options(date.origin = "1970-01-01")
as.date <- function(x, origin = getOption("date.origin")){
origin <- ifelse(is.null(origin), "1970-01-01", origin)
as.Date(x, origin)
}
## Results: (When date.origin is not set)
## > as.date(0)
## [1] "1970-01-01"
## > as.date(2)
## [1] "1970-01-03"
## Results: (When date.origin is set)
## > options(date.origin = "1970-01-05")
## > as.date(2)
## [1] "1970-01-07"
I have a datetime supplied in the following format:
ex <-2008123118
and am trying to convert it to datetime format. I try:
mytime2 <- as.POSIXct(ex, format = "%Y%m%d%H")
but receive the error:
Error in as.POSIXct.numeric(ex, format = "%Y%m%d%H") :
'origin' must be supplied
The error seems straightfoward, but the only issue is what if I don't know the origin. For example, this part of my code will go into a much larger code that will process a number of files.
I would like my example to return:
2008-12-31 18:00
Is that possible?
There are two versions of as.POSIXct working behind the scenes, as.POSIXct.numeric and as.POSIXct.character, and you have tried to call both of them at once. Because that value is coming in as "numeric" it gets passed to as.POSIXct.numeric and that is actually the cause of the error here, because as.POSIXct.numeric has no detection algorithm for date-like values and also has no "format" parameter. Coerce it to "character and all is well:
> ex <-2008123118
> mytime2 <- as.POSIXct(as.character(ex), format = "%Y%m%d%H")
> mytime2
[1] "2008-12-31 18:00:00 PST"
Here is an example with lubridate with the ymd_h function:
library(lubridate)
ymd_h(ex)
[1] "2008-12-31 18:00:00 UTC"
here a base R solution
strptime(ex, format = "%Y%m%d%H")
[1] "2008-12-31 18:00:00 CET"
You can try the following code, in base R:
> ex <-2008123118
> as.POSIXct(strptime(ex, format = "%Y%m%d%H")) #this gives you additional seconds
"2008-12-31 18:00:00 EST"
> strftime(as.POSIXct(strptime(ex, format = "%Y%m%d%H")), format = "%Y-%m-%d %H:%M") #this gives your desired format
"2008-12-31 18:00"
Hope this helps.
I have a numeric vector as follows
aa <- c(1022011, 2022011, 13022011, 23022011) (this vector is just a sample, it is very long)
Values are written in such a way that first value is day then month and then year.
What I am doing right now is
as.Date(as.character(aa), %d%m%Y")
but,
it is causing problems (returning NA) in case of single digits day numbers. (i.e. 1022011, 2022011).
so basically
as.Date("1022011", "%d%m%Y") does not work
but
as.Date("01022011", "%d%m%Y") (pasting '0' ahead of the number) works.
I want to avoid pasting '0' in such cases. Is there any other (direct) alternative to convert numeric values to dates at once?
It could be rearranged using sub in which case a plain as.Date with no format works:
x <- c(1022011, 11022011) # test data
pat <- "^(..?)(..)(....)$"
as.Date(sub(pat, "\\3-\\2-\\1", x))
giving:
[1] "2011-02-01" "2011-02-11"
Depending on your platform, you could use sprintf in order to add a zero at the beginning. It seems that Mac is OK with this, but not windows 7 given the discussion with the OP.
aa <- c(1022011, 2022011, 13022011, 23022011)
as.Date(sprintf("%08s", aa), format = "%d%m%Y")
[1] "2011-02-01" "2011-02-02" "2011-02-13" "2011-02-23"
UPDATE
#CathyG kindly mentioned that sprintf("%08i",aa) works on Windows 7.
You can use dmy in lubridate:
library(lubridate)
aa <- c(1022011, 2022011, 13022011, 23022011)
> dmy(aa)
[1] "2011-02-01 UTC" "2011-02-02 UTC" "2011-02-13 UTC" "2011-02-23 UTC"
and if you don't want the timezone just wrap it in as.Date:
> as.Date(dmy(aa))
[1] "2011-02-01" "2011-02-02" "2011-02-13" "2011-02-23"
Thank you #Ben Bolker,
> as.Date(mdy(aa))
[1] "2011-01-02" "2011-02-02" "2012-01-02" "2011-01-02"
I know you don't want to add a "0" but still, in base R, this works :
as.Date(sapply(aa,function(x){ifelse(nchar(x)==8,x,paste("0",x,sep=""))}),format = "%d%m%Y")
I am trying to convert the string "2013-JAN-14" into a Date as follow :
sdate1 <- "2013-JAN-14"
ddate1 <- as.Date(sdate1,format="%Y-%b-%d")
ddate1
but I get :
[1] NA
What am I doing wrong ? should I install a package for this purpose (I tried installing chron) .
Works for me. The reasons it doesn't for you probably has to do with your system locale.
?as.Date has the following to say:
## This will give NA(s) in some locales; setting the C locale
## as in the commented lines will overcome this on most systems.
## lct <- Sys.getlocale("LC_TIME"); Sys.setlocale("LC_TIME", "C")
x <- c("1jan1960", "2jan1960", "31mar1960", "30jul1960")
z <- as.Date(x, "%d%b%Y")
## Sys.setlocale("LC_TIME", lct)
Worth a try.
This can also happen if you try to convert your date of class factor into a date of class Date. You need to first convert into POSIXt otherwise as.Date doesn't know what part of your string corresponds to what.
Wrong way: direct conversion from factor to date:
a<-as.factor("24/06/2018")
b<-as.Date(a,format="%Y-%m-%d")
You will get as an output:
a
[1] 24/06/2018
Levels: 24/06/2018
class(a)
[1] "factor"
b
[1] NA
Right way, converting factor into POSIXt and then into date
a<-as.factor("24/06/2018")
abis<-strptime(a,format="%d/%m/%Y") #defining what is the original format of your date
b<-as.Date(abis,format="%Y-%m-%d") #defining what is the desired format of your date
You will get as an output:
abis
[1] "2018-06-24 AEST"
class(abis)
[1] "POSIXlt" "POSIXt"
b
[1] "2018-06-24"
class(b)
[1] "Date"
My solution below might not work for every problem that results in as.Date() returning NA's, but it does work for some, namely, when the Date variable is read in in factor format.
Simply read in the .csv with stringsAsFactors=FALSE
data <- read.csv("data.csv", stringsAsFactors = FALSE)
data$date <- as.Date(data$date)
After trying (and failing) to solve the NA problem with my system locale, this solution worked for me.