I have a dataframe in long table format like this one below:
mydf <- data.frame(id = c(123, 123, 123),
name =c("test_2001", "test_area_2002", "test_area_sqkm_2003"),
value = c(15, 20, 25))
mydf
#> id name value
#> 1 123 test_2001 15
#> 2 123 test_area_2002 20
#> 3 123 test_area_sqkm_2003 25
After taking reference from this question here, I applied function separate
from package tidyr
:
library(dplyr)
library(tidyr)
mydf %>%
separate(name, c("name","year"), extra="merge", fill = "left")
#> id name year value
#> 1 123 test 2001 15
#> 2 123 test area_2002 20
#> 3 123 test area_sqkm_2003 25
But I couldn't figure out how to split characters and numbers into two different columns like this:
desired.df
#> id name year value
#> 1 123 test 2001 15
#> 2 123 test_area 2002 20
#> 3 123 test_area_sqkm 2003 25
Created on 2021-12-15 by the reprex package (v2.0.0)