1
getDecade <- function(year) {
  case_when(
    year < 1900 ~ "1890s",
    year >= 1900 && year < 1910 ~ "1900s",
    year >= 1910 && year < 1920 ~ "1910s",
    year >= 1920 && year < 1930 ~ "1920s",
    year >= 1930 && year < 1940 ~ "1930s",
    year >= 1940 && year < 1950 ~ "1940s",
    year >= 1950 && year < 1960 ~ "1950s",
    year >= 1960 && year < 1970 ~ "1960s",
    year >= 1970 && year < 1980 ~ "1970s",
    year >= 1980 && year < 1990 ~ "1980s",
    year >= 1990 && year < 2000 ~ "1990s",
    year >= 2000 && year < 2010 ~ "2000s",
    TRUE ~ "other"
  )
}

# (b) Check that your decade function works as intended.
movies <- ggplot2movies::movies %>% 
  mutate (decade = getDecade(year))
print(unique(movies$decade))

The output is: [1] "1970s" "1890s"

Why is almost every movie from 1970s??? What is happening???

Imme
  • 15
  • 4
  • 3
    Use single `&` instead of `&&`. Second form is non vectorized form for code block conditionals like if, while... – Ric Oct 20 '22 at 13:47
  • 3
    This is a pretty verbose way of writing this. Check out the `cut` function as a more ergonomic alternative. You could also use a combination of `round` and `paste0` (i.e. `paste0(round(year, -1L), "s")`). – Konrad Rudolph Oct 20 '22 at 13:49
  • this is a perfect case for `findInterval()` – Ottie Oct 20 '22 at 15:06

1 Answers1

0

As Ric said, the problem was &&. See here for more info

Good code:

getDecade <- function(year) {
  case_when(
    year < 1900 ~ "1890s",
    year >= 1900 & year < 1910 ~ "1900s",
    year >= 1910 & year < 1920 ~ "1910s",
    year >= 1920 & year < 1930 ~ "1920s",
    year >= 1930 & year < 1940 ~ "1930s",
    year >= 1940 & year < 1950 ~ "1940s",
    year >= 1950 & year < 1960 ~ "1950s",
    year >= 1960 & year < 1970 ~ "1960s",
    year >= 1970 & year < 1980 ~ "1970s",
    year >= 1980 & year < 1990 ~ "1980s",
    year >= 1990 & year < 2000 ~ "1990s",
    year >= 2000 & year < 2010 ~ "2000s",
    TRUE ~ "other"
  )
}

Better code:

movies %>% 
  mutate(decade = cut(year, breaks = seq(1890, 2010, 10), include.lowest = TRUE, labels = paste0(seq(1890, 2000, 10), "s")))

Credit to Konrad for the cut idea

Mark
  • 7,785
  • 2
  • 14
  • 34