I would write a function that returns either the column or NULL and then use one of the built in functions like lapply() or map() that iterate over a list. A data frame is just a list with some extra features.
MyFunc <- function(Col) {
if (is.numeric(Col)) {
return(Col)
} else {
return(NULL)
}
}
DF <- data.frame(N1 = 1:4, C1 = LETTERS[1:4],
N2 = 10:13, C2 = LETTERS[5:8])
DF
#> N1 C1 N2 C2
#> 1 1 A 10 E
#> 2 2 B 11 F
#> 3 3 C 12 G
#> 4 4 D 13 H
DF[] <- lapply(DF, MyFunc)
DF
#> N1 N2
#> 1 1 10
#> 2 2 11
#> 3 3 12
#> 4 4 13
DF <- data.frame(N1 = 1:4, C1 = LETTERS[1:4],
N2 = 10:13, C2 = LETTERS[5:8])
library(purrr)
DF <- map_dfc(DF, MyFunc)
DF
#> # A tibble: 4 x 2
#> N1 N2
#> <int> <int>
#> 1 1 10
#> 2 2 11
#> 3 3 12
#> 4 4 13
Created on 2022-05-04 by the reprex package (v2.0.1)