I know that the question is very easy, but I have a more specific one:
I have a data frame, with 50 variables (numeric and non-numeric) and 5000 observations.
Now what I want to do is create another data frame containing only the numerica variables of the original one.
On this website I found the solution of my problem, that is:
numeric_variables<-unlist(lapply(original_data,is.numeric))
X<-original_data[numeric_variables]
But I was wondering: why if I try like this, it does not work instead? what's wrong?
numeric_variables2<-apply(original_data,2,is.numeric)
x<-original_data[numeric_variables2]
applyconverts tomatrixand matrix can have a single type so, it is essentially converting to character type if there are character columns. Checkcbind(1:5, LETTERS[1:5])with tidyverse, it isoriginal_data %>% select_if(is.numeric)oriris %>% select(where(is.numeric))Filter(is.numeric, df1)iris[mapply(is.numeric, iris)]for exampleapplyconverts things to a matrix, which loses class information. With akrun's comment, hopefully that's enough to steer anyone finding this question in the right direction.