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]
5
  • 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)) Commented Jun 16, 2020 at 19:15
  • 1
    Try Filter(is.numeric, df1) Commented Jun 16, 2020 at 19:15
  • 1
    even better would be iris[mapply(is.numeric, iris)] for example Commented Jun 16, 2020 at 19:17
  • @markus,doesnt work Commented Jun 16, 2020 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. Commented Jun 16, 2020 at 19:42

1 Answer 1

3

try this :

names_num <- names(which(sapply(df, is.numeric)))
df_num <- df[, names_num]
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.