Possible to use starts_with with mutate to convert variable type (i.e. character -> numeric)?

For all columns that start with a certain prefix (e.g. numeric_column_1, numeric_covariate_2, numeric_feature_3 etc.), I would like to convert them into numeric using as.numeric(). Normally, this is would be the manual way to do it (assuming there are only few columns, which is doable manually)

df |>
  mutate(numeric_column_1 = as.numeric(numeric_column_1)...)

However, if there are millions of columns, naming them one by one manually wouldn't be feasible. I would like to use starts_with('numeric_')and convert all of them using as.numeric(). However, all the examples I was able to find was using select() and not mutate().

Is this possible?

dat <- data.frame(
  site =
    c("TPB", "TPB", "HBC", "HBC", "CKB", "CKB"),
  numeric_lat =
    c("42.00333", "42.00333", "42.02889", "42.02889", "42.04944", "42.04944"),
  numeric_lng =
    c("73", "73", "70", "70", "71", "71"),
  numeric_elev =
    c("389", "389", "8", "8", "152", "152"),
  numeric_nspp =
    c("6", "5", "16", "6", "18", "14"),
  habitat =
    c("forest", "bog", "forest", "bog", "forest", "bog" ))

str(dat)
#> 'data.frame':    6 obs. of  6 variables:
#>  $ site        : chr  "TPB" "TPB" "HBC" "HBC" ...
#>  $ numeric_lat : chr  "42.00333" "42.00333" "42.02889" "42.02889" ...
#>  $ numeric_lng : chr  "73" "73" "70" "70" ...
#>  $ numeric_elev: chr  "389" "389" "8" "8" ...
#>  $ numeric_nspp: chr  "6" "5" "16" "6" ...
#>  $ habitat     : chr  "forest" "bog" "forest" "bog" ...
to_change <- c(grep("^numeric",colnames(dat)))

for(i in seq_along(dat)) if(i %in% to_change) dat[i][[1]] = as.numeric(dat[i][[1]])
str(dat)
#> 'data.frame':    6 obs. of  6 variables:
#>  $ site        : chr  "TPB" "TPB" "HBC" "HBC" ...
#>  $ numeric_lat : num  42 42 42 42 42 ...
#>  $ numeric_lng : num  73 73 70 70 71 71
#>  $ numeric_elev: num  389 389 8 8 152 152
#>  $ numeric_nspp: num  6 5 16 6 18 14
#>  $ habitat     : chr  "forest" "bog" "forest" "bog" ...
1 Like

generally

dat |> mutate(across(starts_with("numeric"),~as.numeric(.x)))

or because as.numeric takes only one input

dat |> mutate(across(starts_with("numeric"), as.numeric))
1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.