I'm not sure what is wrong with this mutate function. When using this line of code, it doesn't change the values to the wanted values.
dataADstudies_1639_5cols <- dataADstudies_1639_5cols_2D %>%
mutate(Activity = if_else(Parameter == grepl('ANT_L', Parameter), "Phot_Ant_TL", Activity))
My df looks like this:
Nr Section Activity Parameter Value
44 1639_1 Day 0 Photo_2D ANT_L_Ery 18.932
45 1639_1 Day 0 Photo_2D ANT_L_EryRat 1.189
46 1639_1 Day 0 Photo_2D ANT_L_Rough 9.552
47 1639_1 Day 0 Photo_2D ANT_NL_Ery 0.583
48 1639_1 Day 0 Photo_2D ANT_NL_EryRa 0.886
49 1639_1 Day 0 Photo_2D ANT_NL_Rough 3.958
I want to recode values in the activity column to Phot_Ant_TL when the parameter value contains ANT_L and subsequently change values in the activity column to Phot_Ant_NL when the parameter value contains ANT_NL but I think I need a separate line of code for that
Desired result:
Nr Section Activity Parameter Value
44 1639_1 Day 0 Phot_Ant_TL ANT_L_Ery 18.932
45 1639_1 Day 0 Phot_Ant_TL ANT_L_EryRat 1.189
46 1639_1 Day 0 Phot_Ant_TL ANT_L_Rough 9.552
47 1639_1 Day 0 Phot_Ant_NL ANT_NL_Ery 0.583
48 1639_1 Day 0 Phot_Ant_NL ANT_NL_EryRa 0.886
49 1639_1 Day 0 Phot_Ant_NL ANT_NL_Rough 3.958
CodePudding user response:
Try using grepl
like this -
library(dplyr)
dataADstudies_1639_5cols <- dataADstudies_1639_5cols_2D %>%
mutate(Activity = case_when(grepl('ANT_L', Parameter) ~ "Phot_Ant_TL",
grepl('ANT_NL', Parameter) ~ "Phot_Ant_NL",
TRUE ~ Activity))
dataADstudies_1639_5cols
# Nr Section Activity Parameter Value
#1 1639_1 Day0 Phot_Ant_TL ANT_L_Ery 18.932
#2 1639_1 Day0 Phot_Ant_TL ANT_L_EryRat 1.189
#3 1639_1 Day0 Phot_Ant_TL ANT_L_Rough 9.552
#4 1639_1 Day0 Phot_Ant_NL ANT_NL_Ery 0.583
#5 1639_1 Day0 Phot_Ant_NL ANT_NL_EryRa 0.886
#6 1639_1 Day0 Phot_Ant_NL ANT_NL_Rough 3.958
data
It is easier to help if you provide data in a reproducible format
dataADstudies_1639_5cols_2D <- structure(list(Nr =
c("1639_1", "1639_1", "1639_1", "1639_1",
"1639_1", "1639_1"), Section = c("Day0", "Day0", "Day0", "Day0",
"Day0", "Day0"), Activity = c("Photo_2D", "Photo_2D", "Photo_2D",
"Photo_2D", "Photo_2D", "Photo_2D"), Parameter = c("ANT_L_Ery",
"ANT_L_EryRat", "ANT_L_Rough", "ANT_NL_Ery", "ANT_NL_EryRa",
"ANT_NL_Rough"), Value = c(18.932, 1.189, 9.552, 0.583, 0.886,
3.958)), class = "data.frame", row.names = c(NA, -6L))
CodePudding user response:
with case_when instead of if_else and substr instead of grepl it works:
dataADstudies_1639_5cols <- dataADstudies_1639_5cols_2D %>%
mutate(Activity = case_when(substr(Parameter, 1 , 6) == "ANT_NL" ~ "Phot_Ant_NL",
T ~ "Phot_Ant_TL"))`