my df looks like this containing one column
df <- c("1:a1_01_01","2:a2_03_03","3:a3_07_07")
I want to add two more columns in this dataframe by recoding existing column like following
column1: ("1:a1", "2:a2", "3:a3")
column2:("a1","a2","a3")
>Solution :
We can use trimws
library(dplyr)
df %>%
mutate(column1 =trimws(col1, whitespace = "_.*"),
column2 = trimws(column1, whitespace = "\\d+:"))
data
df <- structure(list(col1 = c("1:a1_01_01", "2:a2_03_03", "3:a3_07_07"
)), class = "data.frame", row.names = c(NA, -3L))