I'm trying to correct errors made in the field when field workers were observing which tree species (SORTNR) were planted in which sites (Siteid). Unfortunately, the mistakes which were made are not the same across sites.
What I am trying to express in my code is: When the Siteid and SORTNR are a specific combination, replace the SORTNR with the correct value. However, when I then inspect the data, all SORTNR are NA.
If I break it down and run only one of the recoding blocks, it appears as if the variable SORTNR for the combinations not included in the call are set to NA, and that running both blocks will lead to all combinations being set to NA.
How do I prevent not mentioned combinations being changed to NA? Can we make it unnecessary to explicitly state that I want to replace correct values with themselves?
Sample data:
Siteid <- c(rep("F410", 10), "F411","F411","F411","F411","F411")
SORTNR <- c(1,2,4,5,8,9,10,11,12,2,12,14,28,15,12)
Dataframe <- data.frame(cbind(Siteid,SORTNR))
Recoding
#Recoding Block 1
Dataframe <- Dataframe %>% mutate(SORTNR=case_when(
Siteid=="F410" & SORTNR==1~2,
Siteid=="F410" & SORTNR==2~2,
Siteid=="F410" & SORTNR==4~28,
Siteid=="F410" & SORTNR==5~28,
Siteid=="F410" & SORTNR==8~28,
Siteid=="F410" & SORTNR==9~28,
Siteid=="F410" & SORTNR==10~27,
Siteid=="F410" & SORTNR==11~28,
Siteid=="F410" & SORTNR==12~28))
#Recoding Block 2
Dataframe <- Dataframe %>% mutate(SORTNR=case_when(
Siteid=="F411" & SORTNR==12~13,
Siteid=="F411" & SORTNR==28~29,
Siteid=="F411" & SORTNR==14~14,
Siteid=="F411" & SORTNR==15~15
Values that don't have a match in a case_when()
statement are assigned NA
so you need to use TRUE ~ SORTNR
as the final condition to avoid this.
library(dplyr)
Dataframe %>%
mutate(SORTNR = case_when(Siteid=="F410" & SORTNR %in% c(1,2) ~ 2,
Siteid=="F410" & SORTNR %in% c(4,5,8,9,11,12) ~ 28,
Siteid=="F410" & SORTNR == 10 ~ 27,
Siteid=="F411" & SORTNR == 12 ~ 13,
Siteid=="F411" & SORTNR == 28 ~ 29,
Siteid=="F411" & SORTNR == 14 ~ 14,
Siteid=="F411" & SORTNR == 15 ~ 15,
TRUE ~ SORTNR))