R is giving the wrong max date [duplicate] - r

This question already has answers here:
as.Date with dates in format m/d/y in R
(4 answers)
Closed 2 years ago.
I am having an annoying issue in R that is not recognizing the correct date in my dataframe as the max date. I am creating a weighted average based on subtracting the date in the dataframe from the max date, so this is an annoying issue to deal with. I have tried reformatting all of the dates, this did not work.
Anything that I can try? The dates range from 1/1/2020 to 12/23/2020. But when I use max(PlayerData$Date) I am getting 3/9/2020.
Thanks

The issue is that max() is not working for characters. You need to transform it to date class. Here an example:
#Data
PlayerData <- data.frame(Date=c('3/9/2020','1/1/2020','12/23/2020'),stringsAsFactors = F)
Your approach:
#OP
max(PlayerData$Date)
Output:
max(PlayerData$Date)
[1] "3/9/2020"
Setting as date:
#Date
max(as.Date(PlayerData$Date,'%m/%d/%Y'))
Output:
max(as.Date(PlayerData$Date,'%m/%d/%Y'))
[1] "2020-12-23"

Related

Converting YY/MM/DD to MM/DD/YY in R [duplicate]

This question already has answers here:
How to convert date to format "yyyy-mm-dd" in R when input value can be of different formats
(3 answers)
Change Date print format from yyyy-mm-dd to dd-mm-yyyy
(2 answers)
Closed 2 years ago.
I need to find the difference in days between 2 date columns, but one of them is in the format of "6/16/2019" and the other is in the format of "2019-02-25". Not sure which one would be easier to convert to which, but would like to get end result in days. Which I know how to do. I would appreciate help converting the second yyyy-mm-dd to mm-dd-yyyy.
We can use functions from the lubridate package to convert the different formats to dates, and then subtract.
rawData <- "date1,date2
2002-05-15,6/16/2019
2019-12-31,4/15/2020"
data <- read.csv(text = rawData,stringsAsFactors = FALSE)
library(lubridate)
mdy(data$date2) - ymd(data$date1)
...and the output:
> mdy(data$date2) - ymd(data$date1)
Time differences in days
[1] 6241 106
>

Using as.Date function in R on columns with blank cells and dates [duplicate]

This question already has answers here:
Converting year and month ("yyyy-mm" format) to a date?
(9 answers)
Closed 3 years ago.
I have encountered a difficulty, trying to use the as.Date function (in R) on a data frame to preserve date format. The date column consists of blank cells (i.e. missing dates) and observed dates in the format month/year (e.g. 8/2019).
As mentioned earlier, I have tried using the as.Date function but the column for the dates turns blank completely (i.e. no dates are reported). Below is the code I am using:
df$date <- df$date<- as.Date(df$date, format='%m/%Y') #df is the data frame
The expected results should have the observed dates and the missing dates replaced with NA. I greatly appreciate your help.
You need to add a date component to make it a complete date. Once you do that it is easy to convert it into an actual date object
as.Date(paste0("1/", "8/2019"), "%d/%m/%Y")
#[1] "2019-08-01"
Or using dmy from lubridate
lubridate::dmy(paste0("1/", "8/2019"))

Converting an integer date [duplicate]

This question already has answers here:
How do I convert date to number of days in R
(7 answers)
Closed 4 years ago.
My question doesn't have to do with my own dataset, but since I'm new to R, I wanted to make sure I knew how to work with dates, so I'm searching up the different ways to manipulate and compare dates in R.
I recently read an answer to a question regarding converting a date into an integer date using the as.numeric () function. Here is the answer that was accepted: https://stackoverflow.com/a/8215581/10864249
So from that answer, my understanding is that the date was converted into seconds.
Why would anyone want to use the as.numeric() function if we're going to only get seconds?
Can we convert the integer date into a smaller integer, like # of days by just dividing by 365.25 or by months even by dividing by 12, then? I assume it'd be easier to compare dates that way, rather than in seconds.
Thanks!!
coercing a date object into a numeric object will give you "the number of days since 01/01/1970"
my_date = as.Date('2015-01-01')
my_date
#[1] "2015-01-01"
class(my_date)
# [1] "Date"
as.numeric(my_date)
# [1] 16436

%b-%Y date conversion gives NA [duplicate]

This question already has answers here:
Converting year and month ("yyyy-mm" format) to a date?
(9 answers)
Closed 4 years ago.
I am trying to convert character strings to Dates in R. These are examples of the character strings:
"Aug-1973" "Aug-1974" "Aug-1975" "Aug-1976" "Aug-1977"
I run the following line on date strings similar to the ones above:
exportsDF$Date <- as.Date(as.character(exportsDF$Date), format = "%b-%Y")
This returns NAs for all values. The step where I convert the dates column to characters returns the correct values. Any ideas why the as.Date() command is not working? There are no NAs or missing values in the data. Every value has a "%b-%Y" format.
Any help is appreciated!
The date format needs a day as well, so you could add an arbitrary day of the month. Here, I've chosen the first day:
dates <- c("Aug-1973", "Aug-1974", "Aug-1975", "Aug-1976", "Aug-1977")
res <- as.Date(paste0("01-", dates), format = "%d-%b-%Y")
print(res)
#[1] "1973-08-01" "1974-08-01" "1975-08-01" "1976-08-01" "1977-08-01"
The reason is that the underlying Date data type is an integer counting the days since some reference day. Specifically, the number of days since 1970-01-01. See ?Date.
The Date object res can now be displayed as you please via
format(res, "%B-%Y")
#[1] "August-1973" "August-1974" "August-1975" "August-1976" "August-1977"
or similar.
The month(res) function and its cousins are also helpful. See ?month.

as.Date keeps on returning NAs [duplicate]

This question already has answers here:
How to convert a character string date to date class if day value is missing
(1 answer)
Converting year and month ("yyyy-mm" format) to a date?
(9 answers)
Closed 6 years ago.
So I've been through some Stack Exchange answers, and I can't resolve this.
I have a column in a dataframe that has dates as characters as follows
2011-12
2012-04
2011-10
etc
I would like to convert these to date formats which I have tried to do as follows:
Tots$DatesMerge<-as.Date(Tots$DatesMerge,"%Y-%m")
but I get NA's back all the time.
I tried to do as here but no joy. I'm really not sure what I'm doing wrong.
I'd say as.Date won't be able to work on values where there's no day of the month. You could try with zoo, as long as you don't mind it coming out as a yearmon class:
library( zoo )
as.yearmon( Tots$DatesMerge )
Alternatively, you can specify a day of the month to use as a dummy:
as.Date( paste0( Tots$DatesMerge, "-15" ) )
Edit: there is already an answer and it is a duplicate, but I suppose the explanation can be useful for further readers, so I'll leave it.
Explanation
This comes from the documentation in R, "Dates are represented as the number of days since 1970-01-01, with negative values for earlier dates".
In R, dates are thus dependent on year, month and days or at least an integer that represent the span (in days) from or to 1970-01-01. As such, the base Dates package in R cannot convert the data formated in years and month into dates since there are no days.
Solution
As a consequence, you have the option, if you go with the base R package, to provide a a day that would be used to convert your data.
Tots$DatesMerge <- as.Date(paste0(Tots$DatesMerge,"01"),"%Y-%m-%d")

Resources