library(dplyr)
data(mtcars)
if(FALSE){
# run the following to see behavior with NA values in dataset
mtcars[sample(1:nrow(mtcars), 3), 'cyl'] <- NA
mtcars[sample(1:nrow(mtcars), 5), 'mpg'] <- NA
}
fmtcars <- within(mtcars, {
cyl <- factor(cyl)
am <- factor(am, labels=c('automatic', 'manual'))
vs <- factor(vs)
})
# with and without factor variables
mtcars |> descript()
fmtcars |> descript() # factors/discrete vars omitted
fmtcars |> descript(discrete=TRUE) # discrete variables only
# usual pipe chaining
fmtcars |> select(mpg, wt) |> descript()
fmtcars |> filter(mpg > 20) |> select(mpg, wt) |> descript()
# conditioning with group_by()
fmtcars |> group_by(cyl) |> descript()
fmtcars |> group_by(cyl, am) |> descript()
# conditioning also works with group_by()
fmtcars |> group_by(cyl) |> descript(discrete=TRUE)
fmtcars |> group_by(am) |> descript(discrete=TRUE)
fmtcars |> group_by(cyl, am) |> descript(discrete=TRUE)
# only return a subset of summary statistics
funs <- get_descriptFuns()
sfuns <- funs[c('mean', 'sd')] # subset
fmtcars |> descript(funs=sfuns) # only mean/sd
# add a new functions
funs2 <- c(sfuns,
Q_5 = \(x) quantile(x, .05, na.rm=TRUE),
median= \(x) median(x, na.rm=TRUE),
Q_95 = \(x) quantile(x, .95, na.rm=TRUE))
fmtcars |> descript(funs=funs2)
Run the code above in your browser using DataLab