user249018
user249018

Reputation: 555

Finding the number of columns of a dataset that are of numeric type

I have a data frame df containing columns of different class. I want to get the number of columns that are of numeric type. I tried which(df$(class(df[,i] == "numeric"))), ncol(df[,class(df[,i])!="factor")), both of which resulted in error messages.

Can you help me out ? Thanks.

Upvotes: 1

Views: 38

Answers (2)

akrun
akrun

Reputation: 887028

An option with select

library(dplyr)
df %>%
    select(where(is.numeric)) %>%
    ncol

Upvotes: 1

Wimpel
Wimpel

Reputation: 27732

df <- data.frame( a = letters[1:10], b = 1:10, c = 11:20 )
sum( lapply( df, is.numeric ) == TRUE )

Upvotes: 2

Related Questions