Home > Blockchain >  How can I use the between function with the case_when function?
How can I use the between function with the case_when function?

Time:10-18

Inside the mutate function, using case_when to obtain several responses to a variable, we are asked to convert a range of years into a specific decade, example: all the years of 1970 would become "1970s" and so on with 1980, 1960 and 1950 How can I write the code without having to write every year? Could I use between in this case?

Code I have written (I don´t know if it´s okay):

mutate (wdsf, decades = case_when (year <= 1950 ~ "1950s and below",
                        year == 1960, 1961, 1962, 1963, 1964, 1965, 1966, 1967, 1968, 1969 ~ 
                        "1960s", 
                        year == 1970, 1971, 1972, 1973, 1974, 1975, 1976, 1977, 1978, 1979 ~ 
                        "1970s", 
                        year == 1980, 1981, 1982, 1983, 1984, 1985, 1986, 1987, 1988, 1989 ~ 
                        "1980s") 

CodePudding user response:

In the code, year == should be changed to year %in% as there are multiple elements or may use between

library(dplyr)
wdsf %>%
    mutate(decades = case_when(year <= 1950 ~ "1950s and below",
                               between(year, 1960, 1969) ~ "1960s",
                               year %in% 1970:1979 ~ "1970s",
                               year %in% 1980:1989 ~ "1980s"))

Also, easier option is cut

cut(wdsf$year, c(-Inf, 1960, 1969, 1979, 1989), 
     labels = c("1950s and below", "1960s", "1970s", "1980s"))

CodePudding user response:

Here is an alternative way how to solve your task using integer division See here with an example dataset:

# example dataset
year <- 1935:1989
wdsf <- data.frame(year)

wdsf %>% 
  mutate(decades = ifelse(year <= 1950, "1950s and below",
                          paste0((c(year)%/% 10)*10,'s')))
 year         decades
1  1935 1950s and below
2  1936 1950s and below
3  1937 1950s and below
4  1938 1950s and below
5  1939 1950s and below
6  1940 1950s and below
7  1941 1950s and below
8  1942 1950s and below
9  1943 1950s and below
10 1944 1950s and below
11 1945 1950s and below
12 1946 1950s and below
13 1947 1950s and below
14 1948 1950s and below
15 1949 1950s and below
16 1950 1950s and below
17 1951           1950s
18 1952           1950s
19 1953           1950s
20 1954           1950s
21 1955           1950s
22 1956           1950s
23 1957           1950s
24 1958           1950s
25 1959           1950s
26 1960           1960s
27 1961           1960s
28 1962           1960s
29 1963           1960s
30 1964           1960s
31 1965           1960s
32 1966           1960s
33 1967           1960s
34 1968           1960s
35 1969           1960s
36 1970           1970s
37 1971           1970s
38 1972           1970s
39 1973           1970s
40 1974           1970s
41 1975           1970s
42 1976           1970s
43 1977           1970s
44 1978           1970s
45 1979           1970s
46 1980           1980s
47 1981           1980s
48 1982           1980s
49 1983           1980s
50 1984           1980s
51 1985           1980s
52 1986           1980s
53 1987           1980s
54 1988           1980s
55 1989           1980s
  • Related