I don't quite understand the behavior of apply()
in case of data.frame columns:
apply(mtcars, 2, is.numeric)
# mpg cyl disp hp drat wt qsec vs am gear carb
# TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
apply(iris, 2, is.numeric)
# Sepal.Length Sepal.Width Petal.Length Petal.Width Species
# FALSE FALSE FALSE FALSE FALSE
in both tables we have numeric data, so why the result is different?
Moreover, if I add a column to mtcars
, it changes the outcome for all columns:
mtcars$colA <- 'charA'
apply(mtcars, 2, is.numeric)
# mpg cyl disp hp drat wt qsec vs am gear carb colA
# FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
For my purpose (determine the type of columns), lapply
does the job (lapply(mtcars, is.numeric)
) - but I still would like to figure out what's going on in case of apply(df,2,myfunc)