Lilip
December 21, 2022, 11:45am
1
In this df, a variable includes observations I want to rename as “lof”, “.”, “missense”. As there are many possibilities for the “missense”, I left it after TRUE (else) when using case_when function. The problem is the function is considering NA as “missense”. How can I tell this function to recognize NA as NA?
df <- df %>%
mutate(type_variant = case_when (str_detect(HGVSprot , regex("fs|Ter", ignore_case = TRUE)) ~ "LoF",
str_detect(HGVSprot, "^\\.$") ~".",
TRUE ~"missense"))
jkatam
December 21, 2022, 11:59am
2
I updated the case_when with is.na(HGVSprot) ~ NA, please check
Lilip:
df <- df %>%
mutate(type_variant = case_when (str_detect(HGVSprot , regex("fs|Ter", ignore_case = TRUE)) ~ "LoF",
str_detect(HGVSprot, "^\\.$") ~".",
is.na(HGVSprot) ~ NA,
TRUE ~"missense"))
Lilip
December 21, 2022, 1:01pm
3
Have tried, but it did not work
To make easier:
> HGVSprot <- c("p.(Cys113Ter)","p.(Gln30His)","p.(Ser340Pro)", NA )
> V <- c("a", "b", "c", "d")
> df <- data.frame(HGVSprot, V)
jkatam
December 21, 2022, 1:46pm
4
Lilip:
lof
Please try the below code it worked for me
df <- df %>%
mutate(type_variant = ifelse(!is.na(HGVSprot),case_when (str_detect(HGVSprot , regex("fs|Ter", ignore_case = TRUE)) ~ "LoF",
str_detect(HGVSprot, "^\\.$") ~".",
TRUE ~ "missense"), NA))
Lilip
December 21, 2022, 2:18pm
5
Worked just as perfect.
Good ideia adding ifelse. Thanks.
1 Like
jkatam
December 21, 2022, 2:21pm
6
thank you @Lilip , could you please consider this as a solution
system
Closed
December 28, 2022, 2:22pm
7
This topic was automatically closed 7 days after the last reply. New replies are no longer allowed. If you have a query related to it or one of the replies, start a new topic and refer back with a link.