R Converting an Integer Column to 3 Digit-Based Factor Columns

I have an int column as follows:

idNums 2 101 34 25 8 ... 

I need to convert them to 3 factor columns as follows:

 digit1 digit2 digit3 0 0 2 1 0 1 0 3 4 0 2 5 0 0 8 ... ... ... 

Any suggestions?

+4
source share
3 answers

Here's a fun solution using the modular arithmetic operators %% and %/% :

 d <- c(2, 101, 34, 25, 8) res <- data.frame(digit1 = d %/% 100, digit2 = d %% 100 %/% 10, digit3 = d %% 10) # digit1 digit2 digit3 # 1 0 0 2 # 2 1 0 1 # 3 0 3 4 # 4 0 2 5 # 5 0 0 8 

Note that it has a slight but nice advantage - returning numeric values ​​for each column. However, if you want a coefficient instead of columns, simply run the following command:

 res[] <- lapply(res, as.factor) all(sapply(res, class)=="factor") #[1] TRUE 
+7
source

Use formatC and strsplit .

 idNums <- c(2, 101, 34, 25, 8) idChars <- formatC(idNums, width = 3, flag = "0") idChars <- strsplit(idChars, "") data.frame( digits1 = sapply(idChars, function(x) x[1]), digits2 = sapply(idChars, function(x) x[2]), digits3 = sapply(idChars, function(x) x[3]) ) 

A bit cleaner using the stringr package. Replace the call with strsplit with

 str_split_fixed(idChars, "", 3) 
+5
source

I thought using Richie Cottons in C format was kewl, so I included it:

 testdat <- read.fwf(textConnection(formatC(idNums, width = 3, flag = "0") ), widths=c(1,1,1), col.names=c("digit1", "digit2", "digit3") ) testdat #------------ digit1 digit2 digit3 1 0 0 2 2 1 0 1 3 0 3 4 4 0 2 5 5 0 0 8 
+3
source

Source: https://habr.com/ru/post/1390087/


All Articles