Map array of strings to an array of integers

前端 未结 4 422
灰色年华
灰色年华 2020-12-11 09:15

Suppose I have a column in a data frame as colors say c(\"Red\", \"Blue\", \"Blue\", \"Orange\"). I would like to get it as c(1,2,2,3).

<         


        
4条回答
  •  时光取名叫无心
    2020-12-11 09:40

    Here is a function based on previous code:

    # Recode 'string' into 'integer'
    recode_str_int <- function(df, feature) {
    
      # 1. Unique values
    
      # 1.1. 'string' values
      list_str <- sort(unique(df[, feature]))
    
      # 1.2. 'integer' values
      list_int <- 1:length(list_str)
    
      # 2. Create new feature
    
      # 2.1. Names
      names(list_int) = list_str
      df$feature_new = list_int[df[, feature]]
    
      # 3. Result
      df$feature_new
    
    } # recode_str_int
    

    Call it like:

     df$new_feature <- recode_str_int(df, "feature")
    

提交回复
热议问题