Detect type of string and create new variable accordingly to it

Viewed 67

So i have a dataset of passwords and i would like to create a new column if the password matches one of the following strings (im working with R)

id password   year     lenght
1  1 12345      2001       5
2  2 pass4      2002       5
3  3 angel      2003       5
4  4 pizza      2004       5

pattern for passwords with only letters: "^[a-zA-Z]+$"

pattern for passwords with only numbers: "^[0-9]*$"

pattern for passwords with BOTH numbers and letters : '([0-9].[a-zA-Z])|([a-zA-Z].[0-9])'

So basically i would need a new column named : TYPE with 3 levels (numbers, letters or both)

what I need is this :

id password   year     lenght   Type
1  1 12345      2001       5.   numbers only    
2  2 pass4      2002       5.   both
3  3 angel      2003       5.   letters only
4  4 pizza      2004       5.   letters only
1 Answers

You can use the following solution. Just bear in mind that we put our conditions from the most specific (both) to the most general (either of the two other conditions):

library(dplyr)

df %>%
  mutate(Type = case_when(
    grepl("[A-Za-z]+", password) & grepl("[1-9]+", password) ~ "Both",
    grepl("[A-Za-z]+", password) ~ "Letters Only",
    grepl("[1-9]+", password) ~ "Numbers Only",
    TRUE ~ as.character(password)
  ))

  id password year lenght         Type
1  1    12345 2001      5 Numbers Only
2  2    pass4 2002      5         Both
3  3    angel 2003      5 Letters Only
4  4    pizza 2004      5 Letters Only
Related