Is this reprex below showing the results you saw?
We really need a better idea of what you are trying to do in terms of input and expected output to help you get the results you want.
The thing I think you running into is that your logical test in your second mutate_at is testing a column, i.e. a vector, not the individual element in the column.
Here is what I think you wanted to show and a trimmed down example to showing the difference is in testing individual elements in a column and testing the whole column itself.
Notice that there are a number of warnings about testing the first element of a vector. That's another reason for posting a reprex... we all could have seen that warnings occurred. Those warnings are a clue as to the differences you are seeing.
suppressPackageStartupMessages(library(dplyr))
# does this produce the difference you are talking about?
tbl <- tibble::tribble (
~columnA, ~x, ~y, ~z,
1, "sadf", 8, FALSE,
2, "ssadf", 19, FALSE,
3, "sssadf", 10, TRUE
)
tbl %>% mutate_at(vars(-columnA), funs(((function(x) {
if (is.logical(x))
return(x)
else if (!is.na(as.numeric(x)))
return(as.numeric(x))
else
return(NA)
})(.))))
#> Warning in (function(x) {: NAs introduced by coercion
#> Warning in if (!is.na(as.numeric(x))) return(as.numeric(x)) else
#> return(NA): the condition has length > 1 and only the first element will be
#> used
#> Warning in if (!is.na(as.numeric(x))) return(as.numeric(x)) else
#> return(NA): the condition has length > 1 and only the first element will be
#> used
#> # A tibble: 3 x 4
#> columnA x y z
#> <dbl> <lgl> <dbl> <lgl>
#> 1 1 NA 8 FALSE
#> 2 2 NA 19 FALSE
#> 3 3 NA 10 TRUE
# the reason this is different than the preceeding is
# that the logical test is being done on a vector
# so only the the same first element in the vector is being
# tested for each element that is processed.
# The result is that all the output rows are the
# same because they are all based on a test of the
# first element in each column
tbl %>% mutate_at(vars(-columnA),funs(
ifelse(is.logical(.),.,
ifelse(!is.na(as.numeric(.)),as.numeric(.),NA))))
#> Warning in ifelse(!is.na(as.numeric(x)), as.numeric(x), NA): NAs introduced
#> by coercion
#> # A tibble: 3 x 4
#> columnA x y z
#> <dbl> <lgl> <dbl> <lgl>
#> 1 1 NA 8 FALSE
#> 2 2 NA 8 FALSE
#> 3 3 NA 8 FALSE
# trimmed down equivalent example to make it easier to look at
# here the logical test is being done on each element in the column
tbl %>% mutate_at(vars(-columnA), function(x) { if(is.logical(x)) x else NA})
#> # A tibble: 3 x 4
#> columnA x y z
#> <dbl> <lgl> <lgl> <lgl>
#> 1 1 NA NA FALSE
#> 2 2 NA NA FALSE
#> 3 3 NA NA TRUE
# here the logical test is being done on the whole column,
# not each element in the column
# . is the column
tbl %>% mutate_at(vars(-columnA),funs(ifelse(is.logical(.), ., NA)))
#> # A tibble: 3 x 4
#> columnA x y z
#> <dbl> <lgl> <lgl> <lgl>
#> 1 1 NA NA FALSE
#> 2 2 NA NA FALSE
#> 3 3 NA NA FALSE