1

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

1 Answers1

2

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"
Duck
  • 39,058
  • 13
  • 42
  • 84