Home > OS >  Use ifelse() over whole dataframe
Use ifelse() over whole dataframe

Time:11-09

I have a dataframe that looks like this:

dat <- data.frame(T1 = c('s', 's', 'm', 'l', 'm', 'xl'),
                  T2 = c('s', 's', 'm', 'l', 'm', 'xl'),
                  T3 = c('m', 's', 'm', 'l', 'l', 'xxl'), 
                  T4 = c('m', 'm', 'm', 'l', 'l', 'xxl'),
                  T5 = c('m', 'm', 'm', 'xl', 'l', 'xl'))

Now, I would like to convert these characters into numbers (1-5). Is there some way to do this in a few lines for the whole data frame. If I have to ifelse() statements for each column and each character, I use up a whole lot of lines. Thank you in advance.

CodePudding user response:

With match:

dat[] <- match(unlist(dat), unique(unlist(dat)))

  T1 T2 T3 T4 T5
1  1  1  2  2  2
2  1  1  1  2  2
3  2  2  2  2  2
4  3  3  3  3  4
5  2  2  3  3  3
6  4  4  5  5  4

CodePudding user response:

No ifelse, convert to factor with the appropriate levels, then coerce to integer.

dat <- data.frame(T1 = c('s', 's', 'm', 'l', 'm', 'xl'),
                  T2 = c('s', 's', 'm', 'l', 'm', 'xl'),
                  T3 = c('m', 's', 'm', 'l', 'l', 'xxl'), 
                  T4 = c('m', 'm', 'm', 'l', 'l', 'xxl'),
                  T5 = c('m', 'm', 'm', 'xl', 'l', 'xl'))

dat[] <- lapply(dat, \(x) as.integer(factor(x, levels = c('s', 'm', 'l', 'xl', 'xxl'))))
dat
#>   T1 T2 T3 T4 T5
#> 1  1  1  2  2  2
#> 2  1  1  1  2  2
#> 3  2  2  2  2  2
#> 4  3  3  3  3  4
#> 5  2  2  3  3  3
#> 6  4  4  5  5  4

Created on 2022-11-09 with reprex v2.0.2

CodePudding user response:

library(tidyverse)

df %>%  
  mutate(across(everything(), ~ factor(.x, levels = c("s", "m", "l", "xl", "xxl")) %>% as.integer()))

# A tibble: 6 x 5
     T1    T2    T3    T4    T5
  <int> <int> <int> <int> <int>
1     1     1     2     2     2
2     1     1     1     2     2
3     2     2     2     2     2
4     3     3     3     3     4
5     2     2     3     3     3
6     4     4     5     5     4
  • Related