i'm trying to do some modifications to the next data frame:
df <- data.frame(
zgen = c("100003446", "100001749","100002644","100001755"),
Name_mat = c("EVEROLIMUS 10 MG CM", "GALSULFASA 5MG/5ML FAM", "IDURSULFASE 2MG/ML SOL. P/INFUSION FAM","IMIGLUCERASA 400U POL. LIOF. FAM"),
details= c("CM", "FAM", "SOL. P/INFUSION FAM","NA")
)
And i'm using mutate_at(
) from dplyr
package to create a new column calling "type". That column can change depending of a list of characters that can appear in the columns of my data frame ("name_mat" and "details"). The code is:
df <- df %>% mutate_at(vars(one_of("Name_mat ","details")),
funs(case_when( "FAM|FRA" == TRUE ~ "FA",
"CM|COMPRIMIDO" == TRUE~ "COM",
"SOL"== TRUE~"SOL",
"CP|CAPSULA"== TRUE~"CAP",
TRUE ~ "bad_mat")))
My first time using mutate_at and i don't know how to create a new column calling "type" in my data frame "df". Finally i need something like:
ZGEN Name_mat details Type
1 100003446 EVEROLIMUS 10 MG CM CM COM
2 100001749 GALSULFASA 5MG/5ML FAM FAM FA
3 100002644 IDURSULFASE 2MG/ML SOL. P/INFUSION FAM SOL. P/INFUSION FAM FA
4 100001755 IMIGLUCERASA 400U POL. LIOF. FAM NA FA
I appreciate any help or any other point of view about how to do this.
Thanks!