Home > Mobile >  Rename string values in a column when multiple conditions are not met
Rename string values in a column when multiple conditions are not met

Time:11-29

I have an age range data column that looks like this:

data <- c("<18", "956", "675", "18-30", "31-50", "50 ", "543" "Unknown")

Now, I want to rename the all whole number characters as "Unknown" with the new column looking like:

data <- c("<18", "18-30", "31-50", "50 ", "Unknown")

I have tried using this:

if(data$age_range !="<18" || data$age_range !="18-30" || data$age_range !="31-50" || data$age_range !="50 ") 
{data$age_range2 = "Unknown"}

But it renames all the values to "Unknown"

I also tried using ifelse, but it's not working either. I'm quite new to R and would appreciate all the help I can get.

Thanks in advance!

CodePudding user response:

You can convert data to numeric and rename whole numbers as "Unknown".

data <- c("<18", "956", "675", "18-30", "31-50", "50 ", "543", "Unknown")

data[which(!is.na(as.numeric(data)))] <- "Unknown"

data

Result

[1] "<18"     "Unknown" "Unknown" "18-30"   "31-50"   "50 "     "Unknown" "Unknown"

If you want to keep only one "Unknown",

sort(unique(data))
# [1] "<18"     "18-30"   "31-50"   "50 "     "Unknown"
  •  Tags:  
  • r
  • Related