how can I write a short script that creates a new data frame that reports the following descriptive statistics for each column of continuous data for the survey below: mean,
The following use of do.call, rbind and sapply provides a summary for each column that has the class 'numeric'. You can write your own statistics function if you need different statistics than those of summary (see the answer of @Jilber).
mtcars$carb = as.factor(mtcars$carb) # Forcing one column to a factor
do.call('rbind', sapply(mtcars, function(x) if(is.numeric(x)) summary(x)))
Min. 1st Qu. Median Mean 3rd Qu. Max.
mpg 10.400 15.420 19.200 20.0900 22.80 33.900
cyl 4.000 4.000 6.000 6.1880 8.00 8.000
disp 71.100 120.800 196.300 230.7000 326.00 472.000
hp 52.000 96.500 123.000 146.7000 180.00 335.000
drat 2.760 3.080 3.695 3.5970 3.92 4.930
wt 1.513 2.581 3.325 3.2170 3.61 5.424
qsec 14.500 16.890 17.710 17.8500 18.90 22.900
vs 0.000 0.000 0.000 0.4375 1.00 1.000
am 0.000 0.000 0.000 0.4062 1.00 1.000
gear 3.000 3.000 4.000 3.6880 4.00 5.000
Here are some examples using data.table.
I'm using the functions defined in the previous answers.
my.summary <- function(x, na.rm=TRUE){
result <- c(Mean=mean(x, na.rm=na.rm),
SD=sd(x, na.rm=na.rm),
Median=median(x, na.rm=na.rm),
Min=min(x, na.rm=na.rm),
Max=max(x, na.rm=na.rm),
N=length(x))
}
set.seed(123)
df <- data.frame(id = 1:1000,
Distance = rnorm(1000, 50, 100),
Age = rnorm(1000, 50, 100),
Height = rnorm(1000, 50, 100)
)
df$Coning <- as.factor(ifelse(df$Distance > 0, "Yes", "No"))
library(fBasics)
library(data.table)
DT <- data.table(df)
setkey(DT, id)
Group by factor variable "Coning"
DT[,lapply(.SD,my.summary),by="Coning"]
Using my.summary() and basicStats() Just numeric Variables
DT[,lapply(.SD, my.summary),, .SDcols = names(DT)[2:4]]
BS <- DT[,sapply(.SD, basicStats),, .SDcols = names(DT)[2:4]]
BS[, summary := znames]
setnames(BS, 1:3, names(DT)[2:4])
BS
DT[,lapply(.SD, summary),, .SDcols = names(DT)[2:4]]
using summary() Numeric Variable using
DT[,sapply(.SD, function(x) if(is.numeric(x)) summary(x)),, .SDcols = names(DT)[2:4]]
Factor Variable
DT[,sapply(.SD, function(x) if(is.factor(x)) summary(x)),, .SDcols = names(DT)[5]]
Using the quantile function is also quite useful:
DT[,sapply(.SD, function(x) if(is.numeric(x)) quantile(x)),, .SDcols = names(DT)[2:4]]
You can write your own function to get such a summary into a data.frame:
# Defining the function
my.summary <- function(x, na.rm=TRUE){
result <- c(Mean=mean(x, na.rm=na.rm),
SD=sd(x, na.rm=na.rm),
Median=median(x, na.rm=na.rm),
Min=min(x, na.rm=na.rm),
Max=max(x, na.rm=na.rm),
N=length(x))
}
# identifying numeric columns
ind <- sapply(df, is.numeric)
# applying the function to numeric columns only
sapply(df[, ind], my.summary)
Distance Age Height
Mean 58.67200 11.840000 1.9160000
SD 45.48137 4.604168 0.9796626
Median 48.80000 13.500000 1.7000000
Min 8.70000 4.000000 0.6000000
Max 241.80000 19.000000 5.0000000
N 50.00000 50.000000 50.0000000
Or you can use the built-in function basicStats from fBasics package for a more detailed summary:
> library(fBasics)
> basicStats(df[, ind])
Distance Age Height
nobs 50.000000 50.000000 50.000000
NAs 0.000000 0.000000 0.000000
Minimum 8.700000 4.000000 0.600000
Maximum 241.800000 19.000000 5.000000
1. Quartile 28.300000 7.000000 1.125000
3. Quartile 74.675000 15.750000 2.475000
Mean 58.672000 11.840000 1.916000
Median 48.800000 13.500000 1.700000
Sum 2933.600000 592.000000 95.800000
SE Mean 6.432037 0.651128 0.138545
LCL Mean 45.746337 10.531510 1.637583
UCL Mean 71.597663 13.148490 2.194417
Variance 2068.555118 21.198367 0.959739
Stdev 45.481371 4.604168 0.979663
Skewness 1.711028 -0.158853 0.905415
Kurtosis 3.753948 -1.574527 0.578684