1

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]
markus
  • 25,843
  • 5
  • 39
  • 58
Jenny
  • 259
  • 1
  • 2
  • 12
  • 3
    `apply` converts to `matrix` and matrix can have a single type so, it is essentially converting to character type if there are character columns. Check `cbind(1:5, LETTERS[1:5])` with tidyverse, it is `original_data %>% select_if(is.numeric)` or `iris %>% select(where(is.numeric))` – akrun Jun 16 '20 at 19:15
  • 1
    Try `Filter(is.numeric, df1)` – markus Jun 16 '20 at 19:15
  • 1
    even better would be `iris[mapply(is.numeric, iris)]` for example – Daniel O Jun 16 '20 at 19:17
  • @markus,doesnt work – Jenny Jun 16 '20 at 19:26
  • Okay, re-closed, keeping Markus's dupe about how to select numeric column, but also adding a dupe that explains that `apply` converts 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. – Gregor Thomas Jun 16 '20 at 19:42

1 Answers1

3

try this :

names_num <- names(which(sapply(df, is.numeric)))
df_num <- df[, names_num]