Here is some data.
structure(list(Name = c("Porbandar", "Porbandar", "Porbandar",
"Porbandar", "Porbandar", "Porbandar", "Porbandar", "Porbandar",
"Porbandar", "Porbandar", "Porbandar", "Porbandar", "Porbandar",
"Porbandar", "Porbandar", "Porbandar", "Porbandar", "Porbandar",
"Porbandar", "Porbandar"), Level = c("CD BLOCK", "CD BLOCK",
"CD BLOCK", "CD BLOCK", "CD BLOCK", "CD BLOCK", "CD BLOCK", "CD BLOCK",
"CD BLOCK", "CD BLOCK", "CD BLOCK", "CD BLOCK", "CD BLOCK", "CD BLOCK",
"CD BLOCK", "CD BLOCK", "CD BLOCK", "CD BLOCK", "CD BLOCK", "CD BLOCK"
), TRU = c("Total", "Rural", "Urban", "Total", "Rural", "Urban",
"Total", "Rural", "Urban", "Total", "Rural", "Urban", "Total",
"Rural", "Urban", "Total", "Rural", "Urban", "Total", "Rural"
), variable = structure(c(1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L,
4L, 4L, 4L, 5L, 5L, 5L, 6L, 6L, 6L, 7L, 7L), levels = c("TOT_WORK_P",
"TOT_WORK_M", "TOT_WORK_F", "MAINWORK_P", "MAINWORK_M", "MAINWORK_F",
"MAIN_CL_P", "MAIN_CL_M", "MAIN_CL_F", "MAIN_AL_P", "MAIN_AL_M",
"MAIN_AL_F", "MAIN_HH_P", "MAIN_HH_M", "MAIN_HH_F", "MAIN_OT_P",
"MAIN_OT_M", "MAIN_OT_F"), class = "factor"), Population = c(83780,
77412, 6368, 56447, 51184, 5263, 27333, 26228, 1105, 66862, 61215,
5647, 52006, 47115, 4891, 14856, 14100, 756, 31635, 31321)), row.names = c(NA,
20L), class = "data.frame")
I want to create two columns from the 'levels' column, named as:
'work_type' and 'pop_division'.
Pop_division shall be formed from the transformation of the last character in observations of levels such that 'P ' gets listed as 'All', 'F' as 'Female', M as 'Male'.
While work_type will include everything except the last character(and the _ before it) so 'MAIN_AL_F' shall be listed as 'MAIN_AL'.