Home > Mobile >  How to recode logicals of dataframes within a list into numbers in R?
How to recode logicals of dataframes within a list into numbers in R?

Time:03-06

I would like to recode logicals of dataframes within a list into numbers like TRUE = 1 and FALSE = 0. How can I do that?

library(dplyr)
set.seed(94756)
mat1 <- matrix(sample(seq(-1,100, 0.11),50, replace = TRUE),ncol = 5) 
mat1 <- as_tibble(mat1)

mat2 <- matrix(sample(seq(-1,100, 0.11),50, replace = TRUE),ncol = 5)  
mat2 <- as_tibble(mat2)

mat3 <- matrix(sample(seq(-1,100, 0.11), 50,replace = TRUE),ncol = 5)  
mat3 <- as_tibble(mat3)

data <- list(mat1, mat2, mat3)

data1 <- purrr::map(data, ~tibble::add_column(., V1_logical = between(.$V2, 20, 60), .after = 'V1'))
r_pre <- lapply(data1, "[", 2)

Thanks in advance!

CodePudding user response:

You can change the logical values to integer values directly.

lapply(r_pre, function(x) transform(x, V1_logical = as.integer(V1_logical)))

#Shorter version using ` ` which does the same.
#lapply(r_pre, function(x) transform(x, V1_logical =  (V1_logical)))

tidyverse version would be -

library(dplyr)
library(purrr)

map(r_pre, ~.x %>% mutate(V1_logical = as.integer(V1_logical)))

CodePudding user response:

lapply(r_pre,\(x) mutate(x, V1_logical = V1_logical^1))

Output:

[[1]]
# A tibble: 10 x 1
   V1_logical
        <dbl>
 1          1
 2          0
 3          1
 4          1
 5          0
 6          1
 7          1
 8          1
 9          1
10          0

[[2]]
# A tibble: 10 x 1
   V1_logical
        <dbl>
 1          1
 2          1
 3          0
 4          1
 5          0
 6          0
 7          1
 8          0
 9          0
10          1

[[3]]
# A tibble: 10 x 1
   V1_logical
        <dbl>
 1          0
 2          0
 3          1
 4          1
 5          1
 6          1
 7          1
 8          1
 9          1
10          1

CodePudding user response:

Another possible solution:

library(tidyverse)

map(data, ~ add_column(.x, V1_logical = 1*between(.x$V2, 20, 60)))

#> [[1]]
#> # A tibble: 10 × 6
#>       V1    V2    V3      V4    V5 V1_logical
#>    <dbl> <dbl> <dbl>   <dbl> <dbl>      <dbl>
#>  1 46.1   47.2 81.9  60.5    96.5           1
#>  2 10.6   12.9 58.3  52.2    40.7           0
#>  3 36.6   38.7 75.0  77.0    58.7           1
#>  4 46.5   53.0 56.8  55.6    28.3           1
#>  5  5.38  84.1 57.8  95.4    16.7           0
#>  6 44.6   50.0 18.7  12.8     9.12          1
#>  7 44.8   56.8  3.18 66.8    65.1           1
#>  8 68.6   27.5 52.2  -0.0100 81.9           1
#>  9 80.0   21.8 25.7  15.0    43.3           1
#> 10 93.2   10.8 22.0  46.1    16.7           0
#> 
#> [[2]]
#> # A tibble: 10 × 6
#>       V1    V2    V3      V4    V5 V1_logical
#>    <dbl> <dbl> <dbl>   <dbl> <dbl>      <dbl>
#>  1  14.1  47.4 61.6  56.9    73.6           1
#>  2  20.9  48.9 21    -0.0100  9.12          1
#>  3  32.6  63.1 56.5  12.9    58.0           0
#>  4  91.7  48.0 88.0  33.3    10.9           1
#>  5  58.7  72.0 53.2  55.3    86.8           0
#>  6  16.3  66.5 40.4  79.6    73.8           0
#>  7  11.5  56.8  0.65 10.2    75.4           1
#>  8  88.8  92.9 84.8  89.4    65.4           0
#>  9  49.9  77.5 55.6  81.3    13.4           0
#> 10  29.4  44.8  0.76 36.8    22.4           1
#> 
#> [[3]]
#> # A tibble: 10 × 6
#>       V1    V2    V3    V4    V5 V1_logical
#>    <dbl> <dbl> <dbl> <dbl> <dbl>      <dbl>
#>  1 11.1   12.3 12.3  77.2   26.3          0
#>  2 89.9   75.3 23.4  58.3   80.5          0
#>  3 78.8   39.4 20.3  94.0   74.9          1
#>  4 76.8   32.2 81.9  88.4   96.4          1
#>  5 61.8   28.3 81.7  79.1   29.2          1
#>  6 11.8   47.2  2.85  9.23  32.2          1
#>  7  2.41  23.0 23.0  29.8   66.4          1
#>  8 61.4   34.6 21.1  16.5   21.6          1
#>  9 98.7   25.2 35.7  49.5   81.5          1
#> 10 66.3   54.6 18.7  72.7   43.6          1

CodePudding user response:

Use pull() to convert tibble to vector:

r_pre.v<- lapply(r_pre,FUN=pull)

Convert logical to numeric: FALSE will be 0, TRUE will be 1:

r_pre01<-lapply(r_pre.v,as.numeric)

Convert from vector to tibble:

r_pre01<-lapply(r_pre01,as_tibble)
  • Related