【问题标题】:How can I use the between function with the case_when function?如何将 between 函数与 case_when 函数一起使用?
【发布时间】:2021-10-17 18:55:59
【问题描述】:

在 mutate 函数中,使用 case_when 获取对变量的多个响应,要求我们将一系列年份转换为特定的十年,例如:1970 年的所有年份都将变为“1970 年代”,以此类推 1980, 1960 年和 1950 年如何编写代码而不必每年都编写?在这种情况下我可以使用 between 吗?

我写的代码(不知道行不行):

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") 

【问题讨论】:

  • 在这种情况下,您甚至可以只使用&lt;=,利用评估案例的顺序。例如,a &lt;- 1:10; case_when(a &lt;= 3 ~ 'one', a &lt;= 6 ~ 'two', a &lt;= 10 ~ 'three')。虽然,人们不同意这是更好还是更糟。偏好问题

标签: r dplyr


【解决方案1】:

在代码中,year == 应更改为year %in%,因为有多个元素或可能使用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"))

另外,更简单的选择是cut

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

【讨论】:

  • ,@akrun: wdsf 来自哪里?
  • @TarJae 这是他们没有显示的 OP 数据集
【解决方案2】:

这是使用整数除法解决任务的另一种方法 请参阅此处的示例数据集:

# 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-09-18
    • 2019-08-16
    • 1970-01-01
    • 2019-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-20
    相关资源
    最近更新 更多