Label Encoder functionality in R?

前端 未结 9 934
你的背包
你的背包 2021-02-06 08:56

In python, scikit has a great function called LabelEncoder that maps categorical levels (strings) to integer representation.

Is there anything in R to do this?

9条回答
  •  渐次进展
    2021-02-06 09:05

    # input P to the function below is a dataframe containing only categorical variables
    numlevel <- function(P) { 
    
    n <- dim(P)[2]
    
    for(i in 1: n) {
    
      m <- length(unique(P[[i]]))
    
    levels(P[[i]]) <- c(1:m)
    
    }
    
    return(P)
    
    }
    
    Q <- numlevel(P) 
    

提交回复
热议问题