I am trying to create a numeric variable that represents the center of a binned variable. The bins are age by decades, e.g. "20-29" and in need to have 25 instead. Below is the R code I am using. I am looking for a simpler code. thanks.
dataMet_F$Age_bx <- ifelse(dataMet_F$AgeBinned == "20-29", 25,
ifelse(dataMet_F$AgeBinned == "30-39", 35,
ifelse(dataMet_F$AgeBinned == "40-49", 45,
ifelse(dataMet_F$AgeBinned == "50-59", 55,
ifelse(dataMet_F$AgeBinned == "60-69", 65,
ifelse(dataMet_F$AgeBinned == "70-79", 75,
NA))))))
CodePudding user response:
Split the string on '-'
, take average of two values and use ceiling
so that values like 34.5 change to 35.
dataMet_F$Age_bx <- sapply(strsplit(dataMet_F$AgeBinned, '-'), function(x)
ceiling(mean(as.numeric(x))))
dataMet_F
# AgeBinned Age_bx
#1 30-39 35
#2 40-49 45
#3 60-69 65
#4 50-59 55
data
It is easier to help if you provide data in a reproducible format
dataMet_F <- data.frame(AgeBinned = c("30-39", "40-49", "60-69", "50-59"))