mutate(method = cur_group_id(),
method = paste0("M", method)) %>%
mutate(method = case_when(
str_detect(variable, "testhc35") ~ "M2",
str_detect(variable, "testhc36") ~ "M2",
str_detect(variable, "testhc37") ~ "M2",
str_detect(variable, "testhi1") ~ "M2",
str_detect(variable, "testhi2") ~ "M2",
str_detect(variable, "testhi3") ~ "M2",
method == "M1" ~ "M1",
str_detect(variable, "testhc38") ~ "M3",
str_detect(variable, "testhc39") ~ "M3",
str_detect(variable, "testhc40") ~ "M3",
str_detect(variable, "testhi4") ~ "M3",
str_detect(variable, "testhi5") ~ "M3",
str_detect(variable, "testhi6") ~ "M3")) %>%
unite(method_trait, c("method", "trait"), sep = "")
我相信我可以用 str_detect() 完全檢測前 6 行,然后將最后 6 行作為一行。但我不知道該怎么做。
uj5u.com熱心網友回復:
您可以更簡潔地使用范圍:
...
str_detect(variable, "testhc3[5-7]") ~ "M2",
str_detect(variable, "testhi[1-3]") ~ "M2",
method == "M1" ~ "M1",
str_detect(variable, "testhc3[8-9]") ~ "M3",
str_detect(variable, "testhc40") ~ "M3"
str_detect(variable, "testhi[4-6]") ~ "M3"
...
然后您可以混合使用 or ( |) 運算子:
...
str_detect(variable, "testh(c3[5-7]|i[1-3])") ~ "M2",
method == "M1" ~ "M1",
str_detect(variable, "testh(c3[8-9]|i[4-6]|c40)") ~ "M3",
...
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/527691.html
下一篇:如何在r中的一組觀察值之間切換值
