NewBee
NewBee

Reputation: 1040

Generate descriptive statistics for different variable types in R

I know there are a plethora of packages/functions such as (janitor) "tabyl" & "pastec" to get the descriptive values of variables, but I don't know how to apply them over only certain columns.

For example

library(pastec)
stat.desc(iris) 

will return the mean/sd etc., for all the variable, but I want to apply it only to the numeric variables. I don't want to subset, because my data set has over 20 columns and the numeric columns are interspersed in varying orders.

Something else I tried is:

library(janitor) 
lapply(iris,tabyl)

Which is great, except that I don't want tabyl applied over all the columns (because columns with 14,000 ID's makes for an ugly output) & my ultimate aim is to throw this into a neat looking excel file.

Any idea's for how I can apply these cool functions for 'numeric' types and 'character'/'factor' types separately? Or to specific columns specified in a vector?

Upvotes: 1

Views: 326

Answers (1)

Margaret Janiczek
Margaret Janiczek

Reputation: 277

{dplyr} package has some neat ways to select numeric, character variables etc.

For example:

library(pastecs)
library(dplyr)

stat.desc(select_if(iris, is.numeric))

Good luck!

Upvotes: 1

Related Questions