as.date

R set default origin for as.Date

烈酒焚心 提交于 2019-12-05 15:19:08
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" 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

R Convert to date from multiple formats

折月煮酒 提交于 2019-12-04 03:58:12
问题 I need to convert a string of dates that is in multiple formats to valid dates. e.g. dates <- c("01-01-2017","02-01-2017","12-01-2016","20160901","20161001", "20161101") > as.Date(dates, format=c("%m-%d-%Y","%Y%m%d")) [1] "2017-01-01" NA "2016-12-01" "2016-09-01" NA "2016-11-01" two dates show as NA 回答1: This is pretty much I wrote the anytime package for: R> dates <- c("01-01-2017","02-01-2017","12-01-2016","20160901","20161001", + "20161101") R> library(anytime) R> anydate(dates) [1] "2017

How to calculate Mean by Date Grouped as Fiscal Quarters

笑着哭i 提交于 2019-12-02 08:07:44
I have the following table: Date Country Class Value 6/1/2010 USA A 45 6/1/2010 Canada A 23 6/1/2010 Brazil B 65 9/1/2010 USA B 47 9/1/2010 Canada A 98 9/1/2010 Brazil B 25 12/1/2010 USA B 14 12/1/2010 Canada A 79 12/1/2010 Brazil A 23 3/1/2011 USA A 84 3/1/2011 Canada B 77 3/1/2011 Brazil A 43 6/1/2011 USA A 45 6/1/2011 Canada A 23 6/1/2011 Brazil B 65 9/1/2011 USA B 47 9/1/2011 Canada A 98 9/1/2011 Brazil B 25 12/1/2011 USA B 14 12/1/2011 Canada A 79 12/1/2011 Brazil A 23 3/1/2012 USA A 84 3/1/2012 Canada B 77 3/1/2012 Brazil A 43 In column "Date" years are divided by the following months -

How to extract correct date from POSIXct element? [duplicate]

守給你的承諾、 提交于 2019-12-02 05:11:44
This question already has an answer here: Date conversion from POSIXct to Date in R 3 answers How can I get the correct date from the first column in my code? test <- data.frame(posixdate = c("2013-05-01 00:59:00", "2013-05-01 01:59:00", "2013-05-01 02:59:00", "2013-05-01 03:59:00")) test$posixdate <- as.POSIXct(test$posixdate, format="%Y-%m-%d %H:%M:%S" ) test$date <- as.Date(test$posixdate) The above code results in: posixdate date 1 2013-05-01 00:59:00 2013-04-30 2 2013-05-01 01:59:00 2013-04-30 3 2013-05-01 02:59:00 2013-05-01 4 2013-05-01 03:59:00 2013-05-01 The first two dates are not

How to extract correct date from POSIXct element? [duplicate]

最后都变了- 提交于 2019-12-02 04:22:30
问题 This question already has answers here : Date conversion from POSIXct to Date in R (3 answers) Closed 4 years ago . How can I get the correct date from the first column in my code? test <- data.frame(posixdate = c("2013-05-01 00:59:00", "2013-05-01 01:59:00", "2013-05-01 02:59:00", "2013-05-01 03:59:00")) test$posixdate <- as.POSIXct(test$posixdate, format="%Y-%m-%d %H:%M:%S" ) test$date <- as.Date(test$posixdate) The above code results in: posixdate date 1 2013-05-01 00:59:00 2013-04-30 2

R - How can I change date format when I plot an xts & zoo object?

大兔子大兔子 提交于 2019-12-02 02:12:35
问题 I am wondering how I can change date format. The code I am working on is following: library(quantmod) getSymbols("AAPL") price_AAPL <- AAPL[,6] plot(price_AAPL, main = "The price of AAPL") This results I want to alter date format from "%m %d %Y" as shown in the graphic to "%b-%d-%Y" So I tried following after searching some tips: plot(price_AAPL, main = "The price of AAPL", xaxt="n") axis.Date(1, at=seq(head(index(price_AAPL),1), tail(index(price_AAPL),1), length.out=5), format="%b-%d-%Y",

R - How can I change date format when I plot an xts & zoo object?

*爱你&永不变心* 提交于 2019-12-02 00:04:21
I am wondering how I can change date format. The code I am working on is following: library(quantmod) getSymbols("AAPL") price_AAPL <- AAPL[,6] plot(price_AAPL, main = "The price of AAPL") This results I want to alter date format from "%m %d %Y" as shown in the graphic to "%b-%d-%Y" So I tried following after searching some tips: plot(price_AAPL, main = "The price of AAPL", xaxt="n") axis.Date(1, at=seq(head(index(price_AAPL),1), tail(index(price_AAPL),1), length.out=5), format="%b-%d-%Y", las=2) But this doesn't help, and doesn't even show any labeling on x-axis. I suppose that I might did

R Convert to date from multiple formats

泪湿孤枕 提交于 2019-12-01 19:13:04
I need to convert a string of dates that is in multiple formats to valid dates. e.g. dates <- c("01-01-2017","02-01-2017","12-01-2016","20160901","20161001", "20161101") > as.Date(dates, format=c("%m-%d-%Y","%Y%m%d")) [1] "2017-01-01" NA "2016-12-01" "2016-09-01" NA "2016-11-01" two dates show as NA This is pretty much I wrote the anytime package for: R> dates <- c("01-01-2017","02-01-2017","12-01-2016","20160901","20161001", + "20161101") R> library(anytime) R> anydate(dates) [1] "2017-01-01" "2017-02-01" "2016-12-01" "2016-09-01" [5] "2016-10-01" "2016-11-01" R> Parse any sane input reliably

as.Date with two-digit years

六月ゝ 毕业季﹏ 提交于 2019-11-29 16:20:21
If I convert the date 10.10.61 (DD.MM.YY) with as.Date(date, format="%d.%m.%y") for some reason it converts it into 2061 -10-10. Is there an elegant way to correct for this or do I have to do it manually by slicing the string and adding "19" in front? I've also tried the zoo package which brings up the same (wrong) result. x = format(as.Date("10.10.61", "%d.%m.%y"), "19%y-%m-%d") x = as.Date(x) x class(x) Note that a single sub will slice the string and prepend the year with 19 so it is not so onerous: as.Date(sub("(..)$", "19\\1", date), "%d.%m.%Y") ## [1] "1961-10-10" chron Alternately, the

as.Date with two-digit years

拈花ヽ惹草 提交于 2019-11-28 12:45:09
问题 If I convert the date 10.10.61 (DD.MM.YY) with as.Date(date, format="%d.%m.%y") for some reason it converts it into 2061 -10-10. Is there an elegant way to correct for this or do I have to do it manually by slicing the string and adding "19" in front? I've also tried the zoo package which brings up the same (wrong) result. 回答1: x = format(as.Date("10.10.61", "%d.%m.%y"), "19%y-%m-%d") x = as.Date(x) x class(x) 回答2: Note that a single sub will slice the string and prepend the year with 19 so