Skip to main content
Mathematics LibreTexts

1.6.9.1.2: Describing...

  • Page ID
    32662
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    \( \newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\)

    ( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\)

    \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)

    \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\)

    \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)

    \( \newcommand{\Span}{\mathrm{span}}\)

    \( \newcommand{\id}{\mathrm{id}}\)

    \( \newcommand{\Span}{\mathrm{span}}\)

    \( \newcommand{\kernel}{\mathrm{null}\,}\)

    \( \newcommand{\range}{\mathrm{range}\,}\)

    \( \newcommand{\RealPart}{\mathrm{Re}}\)

    \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)

    \( \newcommand{\Argument}{\mathrm{Arg}}\)

    \( \newcommand{\norm}[1]{\| #1 \|}\)

    \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)

    \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    \( \newcommand{\vectorA}[1]{\vec{#1}}      % arrow\)

    \( \newcommand{\vectorAt}[1]{\vec{\text{#1}}}      % arrow\)

    \( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vectorC}[1]{\textbf{#1}} \)

    \( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)

    \( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)

    \( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)

    \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    \(\newcommand{\avec}{\mathbf a}\) \(\newcommand{\bvec}{\mathbf b}\) \(\newcommand{\cvec}{\mathbf c}\) \(\newcommand{\dvec}{\mathbf d}\) \(\newcommand{\dtil}{\widetilde{\mathbf d}}\) \(\newcommand{\evec}{\mathbf e}\) \(\newcommand{\fvec}{\mathbf f}\) \(\newcommand{\nvec}{\mathbf n}\) \(\newcommand{\pvec}{\mathbf p}\) \(\newcommand{\qvec}{\mathbf q}\) \(\newcommand{\svec}{\mathbf s}\) \(\newcommand{\tvec}{\mathbf t}\) \(\newcommand{\uvec}{\mathbf u}\) \(\newcommand{\vvec}{\mathbf v}\) \(\newcommand{\wvec}{\mathbf w}\) \(\newcommand{\xvec}{\mathbf x}\) \(\newcommand{\yvec}{\mathbf y}\) \(\newcommand{\zvec}{\mathbf z}\) \(\newcommand{\rvec}{\mathbf r}\) \(\newcommand{\mvec}{\mathbf m}\) \(\newcommand{\zerovec}{\mathbf 0}\) \(\newcommand{\onevec}{\mathbf 1}\) \(\newcommand{\real}{\mathbb R}\) \(\newcommand{\twovec}[2]{\left[\begin{array}{r}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\ctwovec}[2]{\left[\begin{array}{c}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\threevec}[3]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\cthreevec}[3]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\fourvec}[4]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\cfourvec}[4]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\fivevec}[5]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\cfivevec}[5]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\mattwo}[4]{\left[\begin{array}{rr}#1 \amp #2 \\ #3 \amp #4 \\ \end{array}\right]}\) \(\newcommand{\laspan}[1]{\text{Span}\{#1\}}\) \(\newcommand{\bcal}{\cal B}\) \(\newcommand{\ccal}{\cal C}\) \(\newcommand{\scal}{\cal S}\) \(\newcommand{\wcal}{\cal W}\) \(\newcommand{\ecal}{\cal E}\) \(\newcommand{\coords}[2]{\left\{#1\right\}_{#2}}\) \(\newcommand{\gray}[1]{\color{gray}{#1}}\) \(\newcommand{\lgray}[1]{\color{lightgray}{#1}}\) \(\newcommand{\rank}{\operatorname{rank}}\) \(\newcommand{\row}{\text{Row}}\) \(\newcommand{\col}{\text{Col}}\) \(\renewcommand{\row}{\text{Row}}\) \(\newcommand{\nul}{\text{Nul}}\) \(\newcommand{\var}{\text{Var}}\) \(\newcommand{\corr}{\text{corr}}\) \(\newcommand{\len}[1]{\left|#1\right|}\) \(\newcommand{\bbar}{\overline{\bvec}}\) \(\newcommand{\bhat}{\widehat{\bvec}}\) \(\newcommand{\bperp}{\bvec^\perp}\) \(\newcommand{\xhat}{\widehat{\xvec}}\) \(\newcommand{\vhat}{\widehat{\vvec}}\) \(\newcommand{\uhat}{\widehat{\uvec}}\) \(\newcommand{\what}{\widehat{\wvec}}\) \(\newcommand{\Sighat}{\widehat{\Sigma}}\) \(\newcommand{\lt}{<}\) \(\newcommand{\gt}{>}\) \(\newcommand{\amp}{&}\) \(\definecolor{fillinmathshade}{gray}{0.9}\)

    Firstly, look on the basic characteristics of every character:

    Code \(\PageIndex{1}\) (R):

    data <- read.table("data/bugs.txt", h=TRUE)
    summary(data)

    Since SEX and COLOR are categorical, the output in these columns has no sense, but you may want to convert these columns into “true” categorical data. There are multiple possibilities but the simplest is the conversion into factor:

    Code \(\PageIndex{2}\) (R):

    data <- read.table("data/bugs.txt", h=TRUE)
    data1 <- data
    data1$SEX <- factor(data1$SEX, labels=c("female", "male"))
    data1$COLOR <- factor(data1$COLOR, labels=c("red", "blue", "green"))

    (To retain the original data, we copied it first into new object data1. Please check it now with summary() yourself.)

    summary() command is applicable not only to the whole data frame but also to individual characters (or variables, or columns):

    Code \(\PageIndex{3}\) (R):

    data <- read.table("data/bugs.txt", h=TRUE)
    summary(data$WEIGHT)

    It is possible to calculate characteristics from summary() one by one. Maximum and minimum:

    Code \(\PageIndex{4}\) (R):

    data <- read.table("data/bugs.txt", h=TRUE)
    min(data$WEIGHT)
    max(data$WEIGHT)

    ... median:

    Code \(\PageIndex{5}\) (R):

    data <- read.table("data/bugs.txt", h=TRUE)
    median(data$WEIGHT)

    ... mean for WEIGHT and for each character:

    Code \(\PageIndex{6}\) (R):

    data <- read.table("data/bugs.txt", h=TRUE)
    mean(data$WEIGHT)

    and

    Code \(\PageIndex{7}\) (R):

    data <- read.table("data/bugs.txt", h=TRUE)
    colMeans(data)

    ... and also round the result to one decimal place:

    Code \(\PageIndex{8}\) (R):

    data <- read.table("data/bugs.txt", h=TRUE)
    round(colMeans(data), 1)

    (Again, the output of colMeans() has no sense for SEX and COLOR.)

    Unfortunately, the commands above (but not summary()) do not work if the data have missed values (NA):

    Code \(\PageIndex{9}\) (R):

    data <- read.table("data/bugs.txt", h=TRUE)
    data2 <- data
    data2[3, 3] <- NA
    mean(data2$WEIGHT)

    To calculate mean without noticing missing data, enter

    Code \(\PageIndex{10}\) (R):

    data <- read.table("data/bugs.txt", h=TRUE)
    data2 <- data
    mean(data2$WEIGHT, na.rm=TRUE)

    Another way is to remove rows with NA from the data with:

    Code \(\PageIndex{11}\) (R):

    data <- read.table("data/bugs.txt", h=TRUE)
    data2 <- data
    data2.o <- na.omit(data2)

    Then, data2.o will be free from missing values.

    Sometimes, you need to calculate the sum of all character values:

    Code \(\PageIndex{12}\) (R):

    data <- read.table("data/bugs.txt", h=TRUE)
    sum(data$WEIGHT)

    ... or the sum of all values in one row (we will try the second row):

    Code \(\PageIndex{13}\) (R):

    data <- read.table("data/bugs.txt", h=TRUE)
    sum(data[2, ])

    ... or the sum of all values for every row:

    Code \(\PageIndex{14}\) (R):

    data <- read.table("data/bugs.txt", h=TRUE)
    apply(data, 1, sum)

    (These summarizing exercises are here for training purposes only.)

    For the categorical data, it is sensible to look how many times every value appear in the data file (and that also help to know all values of the character):

    Code \(\PageIndex{15}\) (R):

    data <- read.table("data/bugs.txt", h=TRUE)
    table(data$SEX)
    table(data$COLOR)

    Now transform frequencies into percents (100% is the total number of bugs):

    Code \(\PageIndex{16}\) (R):

    data <- read.table("data/bugs.txt", h=TRUE)
    100*prop.table(table(data$SEX))

    One of the most important characteristics of data variability is the standard deviation:

    Code \(\PageIndex{17}\) (R):

    data <- read.table("data/bugs.txt", h=TRUE)
    sd(data$WEIGHT)

    Calculate standard deviation for each numerical column (columns 3 and 4):

    Code \(\PageIndex{18}\) (R):

    data <- read.table("data/bugs.txt", h=TRUE)
    sapply(data[, 3:4], sd)

    If you want to do the same for data with a missed value, you need something like:

    Code \(\PageIndex{19}\) (R):

    data <- read.table("data/bugs.txt", h=TRUE)
    data2 <- data
    sapply(data2[, 3:4], sd, na.rm=TRUE)

    Calculate also the coefficient of variation (CV):

    Code \(\PageIndex{20}\) (R):

    data <- read.table("data/bugs.txt", h=TRUE)
    100*sd(data$WEIGHT)/mean(data$WEIGHT)

    We can calculate any characteristic separately for males and females. Means for insect weights:

    Code \(\PageIndex{21}\) (R):

    data <- read.table("data/bugs.txt", h=TRUE)
    tapply(data$WEIGHT, data$SEX, mean)

    How many individuals of each color are among males and females?

    Code \(\PageIndex{22}\) (R):

    data <- read.table("data/bugs.txt", h=TRUE)
    table(data$COLOR, data$SEX)

    (Rows are colors, columns are males and females.)

    Now the same in percents:

    Code \(\PageIndex{23}\) (R):

    data <- read.table("data/bugs.txt", h=TRUE)
    100*prop.table(table(data$COLOR, data$SEX))

    Finally, calculate mean values of weight separately for every combination of color and sex (i.e., for red males, red females, green males, green females, and so on):

    Code \(\PageIndex{24}\) (R):

    data <- read.table("data/bugs.txt", h=TRUE)
    tapply(data$WEIGHT, list(data$SEX, data$COLOR), mean)

    This page titled 1.6.9.1.2: Describing... is shared under a Public Domain license and was authored, remixed, and/or curated by Alexey Shipunov.

    • Was this article helpful?