Take a vector and check to see if it looks like it contains only integers.

is_integer_like(x)

Arguments

x

a vector of values to check

Value

TRUE or FALSE indicating if all values look like integers - if the vector is not of type character or numeric it will always return FALSE

Examples

library(dplyr)
#> 
#> Attaching package: ‘dplyr’
#> The following objects are masked from ‘package:stats’:
#> 
#>     filter, lag
#> The following objects are masked from ‘package:base’:
#> 
#>     intersect, setdiff, setequal, union
data <-
  tibble(
    x = c(1, 2, 3),
    y = c("4", "5", "6"),
    z = c("a", "b", "c")
  )
data %>%
  mutate(across(where(is_integer_like), as.integer))
#> # A tibble: 3 × 3
#>       x     y z    
#>   <int> <int> <chr>
#> 1     1     4 a    
#> 2     2     5 b    
#> 3     3     6 c