Home > Software engineering >  R how to change character to numeric to fit model efficiently
R how to change character to numeric to fit model efficiently

Time:11-28

I want to set character as integer to fit model. like, there are five continents, how to change these to integer. The for loop feels rather inefficient. And is there a pattern to the number settings? I want to set gender, age range, education level, continent as numeric.

df <-c("Africa","Americas","Asia","Europe","Oceania","Africa","Americas","Asia","Europe","Oceania")
df1 <- c()
for (i in 1:10) {
  if(df[i] == "Africa"){
    df1[i] <- 1
  }else if(df[i]=="Americas"){
    df1[i] <- 2
  }else if(df[i]=="Asia"){
    df1[i] <- 3
  } else if(df[i]=="Europe"){
    df1[i] <- 4
  } else if(df[i]=="Oceania"){
    df1[i] <- 5
  }
}

CodePudding user response:

  1. An option using match.
df <-c("Africa","Americas","Asia","Europe","Oceania","Africa","Americas","Asia","Europe","Oceania")
order <- c("Africa","Americas","Asia","Europe","Oceania")
result <- match(df, order)
#Also 
#result <- match(df, unique(df))
result
#[1] 1 2 3 4 5 1 2 3 4 5
  1. Using factor.
result <- as.integer(factor(df, order))
  •  Tags:  
  • r
  • Related