0

I have a Release date column in my dataset and need to add a column Decade which supposed to have 4 levels of "1980s", "1990s", "2000s", "2010s".

1980s within 1980-01-01 to 1989-12-31

1990s within 1990-01-01 to 1999-12-31 etc.

Sample of Release Date Column

enter image description here

Here is my code so far:

df$Decade <- cut(df$Release, c(1970,1980,1990,2000))
levels(df$Decade) <- c("1980s", "1990s", "2000s", "2010s")

Here's the error I'm getting:

Error in cut.Date(df$Release, 10 + c(1970, 1980, 1990, 2000)) : invalid specification of 'breaks'

Any help will be greatly appreciated.

Ronak Shah
  • 286,338
  • 16
  • 97
  • 143
Pinaypy
  • 37
  • 6
  • 1
    Please add data using `dput` and show the expected output for the same. Please read the info about [how to ask a good question](http://stackoverflow.com/help/how-to-ask) and how to give a [reproducible example](http://stackoverflow.com/questions/5963269). – Ronak Shah Feb 16 '20 at 03:39
  • 2
    Try this solution: https://stackoverflow.com/questions/35352914/floor-a-year-to-the-decade-in-r – Edward Feb 16 '20 at 03:50

2 Answers2

1

For "Date" objects, you can't cut like that. I'm sure there's an R base version, but lubridate can make your life easier, if you don't care too much about the how or if you don't want to learn to do things from scratch.

library(lubridate)

Decade <- format(floor_date(Release, years(x=10)), "%Y")
Edward
  • 8,978
  • 2
  • 9
  • 24
0

One way would be to convert Release into date, extract only first 3 characters of the year. So 199 for 1991 or 198 for 1987 and then add "0s" to get the decade.

df <- data.frame(Release = c('5/21/1980', '12/12/1980', '5/12/1991'))
df$Decade <- paste0(substring(as.Date(x, '%m/%d/%Y'), 1, 3), "0s")
df
#     Release Decade
#1  5/21/1980  1980s
#2 12/12/1980  1980s
#3  5/12/1991  1990s
Ronak Shah
  • 286,338
  • 16
  • 97
  • 143