I have a wide-format longitudinal dataset with a set of variables that indicate what state a participant lived in for each year of the study period. If no participant lived in a given state that year, there is no level for that state in the variable. For example, using a simplified version in which the dataset contains participants from New England states (MA, CT, RI, VT, NH, ME) only:
ID <- c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
state_2000 <- c("MA", "MA", "RI", "VT", "NH", "NH", "ME", "CT", "CT", "ME")
state_2002 <- c("MA", "MA", "RI", "VT", "NH", "NH", "ME", "CT", "CT", "ME")
# participant # 3 moves from RI to MA; RI no longer a level in the subsequent state variables
state_2004 <- c("MA", "MA", "MA", "VT", "NH", "NH", "ME", "CT", "CT", "ME")
state_2006 <- c("MA", "MA", "MA", "VT", "NH", "NH", "ME", "CT", "CT", "ME")
df <- data.frame(ID, state_2000, state_2002, state_2004, state_2006)
print (df)
ID state_2000 state_2002 state_2004 state_2006
1 1 MA MA MA MA
2 2 MA MA MA MA
3 3 RI RI MA MA
4 4 VT VT VT VT
5 5 NH NH NH NH
6 6 NH NH NH NH
7 7 ME ME ME ME
8 8 CT CT CT CT
9 9 CT CT CT CT
10 10 ME ME ME ME
table(df$state_2002, useNA = "always")
CT MA ME NH RI VT <NA>
2 2 2 2 1 1 0
table(df$state_2004, useNA = "always")
CT MA ME NH VT <NA>
2 3 2 2 1 0
I want to create a set of new state variables that have categories for each state (where categories for states where no one lives in that year would be missing), perhaps by using some combination of mutate(), across(), starts_with(), and case_when(). I've tried something like:
df <-
df %>%
mutate(across(starts_with("state_"), as.factor(case_when(
starts_with("state_")=="CT" ~ 1,
starts_with("state_")=="MA" ~ 2,
starts_with("state_")=="ME" ~ 3,
starts_with("state_")== "NH" ~ 4,
starts_with("state_")=="RI" ~ 5,
starts_with("state_")=="VT" ~ 6,
TRUE ~ NA_real_))))
However, this doesn't seem to work, as I get errors like:
Error in `mutate()`:
! Problem while computing `..1 = across(...)`.
Caused by error:
! attempt to select less than one element in integerOneIndex
Does anyone know how to do this? Thank you so much!