Reputation: 555
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
Reputation: 887028
An option with select
library(dplyr)
df %>%
select(where(is.numeric)) %>%
ncol
Upvotes: 1
Reputation: 27732
df <- data.frame( a = letters[1:10], b = 1:10, c = 11:20 )
sum( lapply( df, is.numeric ) == TRUE )
Upvotes: 2