Date retrieved in R

I struggle with dates in R and can do it pretty easily in SPSS, but I would like to stay in R for my project.

I have a date column in my data frame and you want to completely delete the year to leave the month and day. Here is the peak in my source data.

> head(ds$date) [1] "2003-10-09" "2003-10-11" "2003-10-13" "2003-10-15" "2003-10-18" "2003-10-20" > class((ds$date)) [1] "Date" 

I want it".

 > head(ds$date) [1] "10-09" "10-11" "10-13" "10-15" "10-18" "10-20" > class((ds$date)) [1] "Date" 

If possible, I would like to set the first date on October 1, not January 1.

Any help you can provide would be greatly appreciated.

EDIT: I felt I had to add some context. I want to get an idea of ​​the NHL during the season, which begins in October and ends in April. To add to this, I would like to split the plots for each season, which is a separate column in my data frame. Since I want to compare total performance over the season, I think I need to remove part of the year, but maybe I won’t; as I pointed out, I'm struggling with dates in R. What I'm looking for is a plot that compares cumulative performance by relative dates by season and starts on the x axis in October and ends in April.

+4
source share
3 answers

Is this what you are looking for?

 library(ggplot2) ## make up data for two seasons a and b a = as.Date("2010/10/1") b = as.Date("2011/10/1") a.date <- seq(a, by='1 week', length=28) b.date <- seq(b, by='1 week', length=28) ## make up some score data a.score <- abs(trunc(rnorm(28, mean = 10, sd = 5))) b.score <- abs(trunc(rnorm(28, mean = 10, sd = 5))) ## create a data frame df <- data.frame(a.date, b.date, a.score, b.score) df ## Since I am using ggplot I better create a "long formated" data frame df.molt <- melt(df, measure.vars = c("a.score", "b.score")) levels(df.molt$variable) <- c("First season", "Second season") df.molt 

Then I use ggplot2 to build the data:

 ## plot it ggplot(aes(y = value, x = a.date), data = df.molt) + geom_point() + geom_line() + facet_wrap(~variable, ncol = 1) + scale_x_date("Date", format = "%m-%d") 

If you want to change the x axis (for example, the display format), you will probably be interested in scale_date .

enter image description here

+2
source
 > d = as.Date("2003-10-09", format="%Y-%m-%d") > format(d, "%m-%d") [1] "10-09" 
+8
source

You must remember that a date is a number format that represents the number of days that have passed since the "start" of calculating the internal date:

 > str(Date) Class 'Date' num [1:10] 14245 14360 14475 14590 14705 ... 

This is the same as in EXCEL if you want a link. Therefore, the solution with the format is quite correct.

Now, if you want to set the first date of the year as October 1, you can build such an index of the year as follows:

 redefine.year <- function(x,start="10-1"){ year <- as.numeric(strftime(x,"%Y")) yearstart <- as.Date(paste(year,start,sep="-")) year + (x >= yearstart) - min(year) + 1 } 

Test Code:

 Start <- as.Date("2009-1-1") Stop <- as.Date("2011-11-1") Date <- seq(Start,Stop,length.out=10) data.frame( Date=as.character(Date), year=redefine.year(Date)) 

gives

  Date year 1 2009-01-01 1 2 2009-04-25 1 3 2009-08-18 1 4 2009-12-11 2 5 2010-04-05 2 6 2010-07-29 2 7 2010-11-21 3 8 2011-03-16 3 9 2011-07-09 3 10 2011-11-01 4 
+2
source

Source: https://habr.com/ru/post/1345970/


All Articles