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
Post a Comment