Reputation: 67
YDST | Year |
---|---|
0 | 2020 |
1 | 2020 |
2 | 2020 |
3 | 2020 |
4 | 2020 |
5 | 2020 |
6 | 2020 |
7 | 2020 |
8 | 2020 |
9 | 2020 |
10 | 2020 |
0 | 2021 |
1 | 2021 |
2 | 2021 |
3 | 2021 |
4 | 2021 |
5 | 2021 |
6 | 2021 |
7 | 2021 |
8 | 2021 |
9 | 2021 |
10 | 2021 |
I have a data frame ('df') where I want to create a new column ('Students') where 'ns' indicates no students and 's' indicates students. However where 'ns' and 's' occur differs between years based on 'YDST'. For instance, in 2020 'ns' should be indicated from 0-3 and 9-10. In 2021, 'ns' should be indicated from '1-4', '6-7', and '9-10'.
How can I write a nested ifelse statement to not only account for 'Year', but also the different 'YDST' selection criteria between years?
Thank you!
Upvotes: 0
Views: 52
Reputation: 388797
You can use a ifelse
statement to check for the conditions and assign value 'ns'
or 's'
.
transform(df, students = ifelse(Year == 2020 & YDST %in% c(0:3, 9, 10) |
Year == 2021 & YDST %in% c(1:4, 6:7, 9:10), 'ns','s'))
# YDST Year students
#1 0 2020 ns
#2 1 2020 ns
#3 2 2020 ns
#4 3 2020 ns
#5 4 2020 s
#6 5 2020 s
#7 6 2020 s
#8 7 2020 s
#9 8 2020 s
#10 9 2020 ns
#11 10 2020 ns
#...
#...
data
df <- structure(list(YDST = c(0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L,
10L, 0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L), Year = c(2020L,
2020L, 2020L, 2020L, 2020L, 2020L, 2020L, 2020L, 2020L, 2020L,
2020L, 2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 2021L,
2021L, 2021L, 2021L)), row.names = c(NA, -22L), class = "data.frame")
Upvotes: 1
Reputation: 886928
We can create a key/value dataset and do a join
library(dplyr)
keyval <- data.frame(Year = rep(c(2020, 2021), c(5, 8))
val = c(0:3, 9:10, 1:4, 6:7, 9:10))
left_join(df, keyval) %>%
mutate(Students = case_when(YDST %in% val ~ 'ns', TRUE ~ 's'), val = NULL)
Upvotes: 0