0

Trying to find a way to mutate a new column into a dataframe that takes the 'year' column and rounds down to the first 0. The 'year' column is in dttm.

e.g. year = 1928, decade = 1920.

I've tried to mutate, trunc.Date, round.Date, and a few others.

I started by separating the dates:

df3 <- df2 %>% separate('Release Date', into=c("year","month","day"), sep = "-")

But i'm having trouble making a new column that has the corresponding decade next to each year.

df3 %>% mutate(year = round_date(year,"year",10))

I either get an out of bounds error, or an error that there aren't enough obs in the column.

Thanks for your help!

Zabada

-EDIT - the main reasons I want to do this is so that I can plot decades on the Y axis in ggplot and get a better graph.

zabada
  • 67
  • 1
  • 10
  • Hey @Grothendieck, I viewed that question earlier and it does not work for me. Could I please get some more insight? – zabada Nov 10 '17 at 23:24
  • It should work fairly well `mutate(df3, decade = year - (year %% 10))` as in the question linked. – Jake Kaupp Nov 11 '17 at 00:12
  • Hey @JakeKaupp, thanks for your response. Here is the exact error I'm getting: `a2 <- a1 %>% mutate(`year`, decade = year - (year %% 10))` – zabada Nov 11 '17 at 02:53
  • `separate()` generates character columns, so you need to either specify `convert = TRUE` or convert them to integers by yourself. Did you? – yutannihilation Nov 11 '17 at 23:06
  • I guess what you need is something like `parse_date()` or `ymd()` instead of `separate()` since `round_date()` takes date or datetime objects, not integers. Could you try ``df2 %>% mutate(release = round_date(ymd(`Release Date`), years(10)))``? – yutannihilation Nov 11 '17 at 23:27

0 Answers0