Skip to contents

Automatically check a vector, data.frame or list for numeric, logical, date content and change their datatype. Note that factors are skipped in case the user requires character numeric values to be kept as they are.

Usage

chr2num(data)

chr2logical(data)

chr2date(data)

Arguments

data

Vector, data.frame or list

Examples

str(chr2num(c("1", "2", "3")))
#>  num [1:3] 1 2 3
df <- data.frame(A = c("1", "3"), B = c("A", "B"), c = c(pi, pi * 2))
str(chr2num(df))
#> 'data.frame':	2 obs. of  3 variables:
#>  $ A: num  1 3
#>  $ B: chr  "A" "B"
#>  $ c: num  3.14 6.28
lst <- list(A = c("1", "2", "3"), B = c("A", "B", "3"), C = pi, D = 3L)
str(chr2num(lst))
#> List of 4
#>  $ A: num [1:3] 1 2 3
#>  $ B: chr [1:3] "A" "B" "3"
#>  $ C: num 3.14
#>  $ D: int 3
lst2 <- list(layer1 = ":D", layer2 = lst)
str(chr2num(lst2))
#> List of 2
#>  $ layer1: chr ":D"
#>  $ layer2:List of 4
#>   ..$ A: chr [1:3] "1" "2" "3"
#>   ..$ B: chr [1:3] "A" "B" "3"
#>   ..$ C: num 3.14
#>   ..$ D: int 3
str(chr2logical(c(NA, "true", FALSE)))
#>  logi [1:3] NA TRUE FALSE