vendredi 10 juillet 2020

ifelse statement using logical condition in R

I have this data frame

DF
ID    WORD_LIST   
1     APPLE
2     RED
3     SNOW
4     ANKARA
5     RENEW

I would like to select only word starting with "A" or "R"

DF
ID    WORD_LIST    WORDS_A_R  
1     APPLE        APPLE
2     RED          RED
3     SNOW         NA
4     ANKARA       ANKARA
5     RENEW        RENEW

I tried this code

DF %>% mutate(WORDS_A_R = ifelse(grepl("^A" | "^R", WORD_LIST), as.character(WORD_LIST), NA))

But this error occurs

operations are possible only for numeric, logical or complex types

Aucun commentaire:

Enregistrer un commentaire