r - How to write an if-else loop to add a column? -


i have data

x1    1a41  5d12  5b21  8c12  

i want add column x2 data writes a, b, c, or d if x1 contains respective letter.

x1    x2 1a41  5d12  d 5b21  b 8c12  c 

you can use search & replace , remove letter different a,b,c & d:

# example data df <- data.frame(x1= c("1a41", "5b21", "5d12",  "8c12"))  df$x2 <- gsub('.*([a-d]).*','\\1',df$x1) 

Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -