Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here are some examples using <code>data.table</code>. I'm using the functions defined in the previous answers.</p> <pre><code>my.summary &lt;- function(x, na.rm=TRUE){ result &lt;- 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 &lt;- data.frame(id = 1:1000, Distance = rnorm(1000, 50, 100), Age = rnorm(1000, 50, 100), Height = rnorm(1000, 50, 100) ) df$Coning &lt;- as.factor(ifelse(df$Distance &gt; 0, "Yes", "No")) library(fBasics) library(data.table) DT &lt;- data.table(df) setkey(DT, id) </code></pre> <p>Group by factor variable "Coning"</p> <pre><code>DT[,lapply(.SD,my.summary),by="Coning"] </code></pre> <p>Using <strong>my.summary() and basicStats()</strong> Just numeric Variables</p> <pre><code>DT[,lapply(.SD, my.summary),, .SDcols = names(DT)[2:4]] BS &lt;- 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]] </code></pre> <p>using <strong>summary()</strong> Numeric Variable using</p> <pre><code>DT[,sapply(.SD, function(x) if(is.numeric(x)) summary(x)),, .SDcols = names(DT)[2:4]] </code></pre> <p>Factor Variable</p> <pre><code>DT[,sapply(.SD, function(x) if(is.factor(x)) summary(x)),, .SDcols = names(DT)[5]] </code></pre> <p>Using the quantile function is also quite useful:</p> <pre><code>DT[,sapply(.SD, function(x) if(is.numeric(x)) quantile(x)),, .SDcols = names(DT)[2:4]] </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload