How to get the mean of a single column of data

Hi, i have a set of data with 8 columns. I am new to this area of coding and I am trying to learn as i go along here.

How would i take the mean and IQR of just using the 2nd column of data only? This column of data is called Age. WHat do i type into R to get this up?

I have tried mean (age,2) and mean(age) and error message comes each time. Can anyone help?

Thanks in advance

Using some ploy data, below:

library(tidyverse)

age <- tibble::tibble(
  age = c(23, 54, 33, 67, 43, 23, 56, 78, 54),
  age_2 = c(23, 54, 33, 67, 43, 23, 56, 78, 54)
)

age %>% 
  select(age) %>% 
  summarise(mean = mean(age),
            IQR = IQR(age))
#> # A tibble: 1 × 2
#>    mean   IQR
#>   <dbl> <dbl>
#> 1  47.9    23

Created on 2021-12-06 by the reprex package (v2.0.1)

Outside of the tidyverse, the most simple, base-R way of doing this is by indexing your data frame.

If your data frame looks like this:

tibble::tibble(iris)
#> # A tibble: 150 x 5
#>    Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#>           <dbl>       <dbl>        <dbl>       <dbl> <fct>  
#>  1          5.1         3.5          1.4         0.2 setosa 
#>  2          4.9         3            1.4         0.2 setosa 
#>  3          4.7         3.2          1.3         0.2 setosa 
#>  4          4.6         3.1          1.5         0.2 setosa 
#>  5          5           3.6          1.4         0.2 setosa 
#>  6          5.4         3.9          1.7         0.4 setosa 
#>  7          4.6         3.4          1.4         0.3 setosa 
#>  8          5           3.4          1.5         0.2 setosa 
#>  9          4.4         2.9          1.4         0.2 setosa 
#> 10          4.9         3.1          1.5         0.1 setosa 
#> # ... with 140 more rows

You can access individual columns of your data frame by using the $ notation. This returns a vector, the data structure you need for the base statistical summary functions.

iris$Sepal.Length
#>   [1] 5.1 4.9 4.7 4.6 5.0 5.4 4.6 5.0 4.4 4.9 5.4 4.8 4.8 4.3 5.8 5.7 5.4 5.1
#>  [19] 5.7 5.1 5.4 5.1 4.6 5.1 4.8 5.0 5.0 5.2 5.2 4.7 4.8 5.4 5.2 5.5 4.9 5.0
#>  [37] 5.5 4.9 4.4 5.1 5.0 4.5 4.4 5.0 5.1 4.8 5.1 4.6 5.3 5.0 7.0 6.4 6.9 5.5
#>  [55] 6.5 5.7 6.3 4.9 6.6 5.2 5.0 5.9 6.0 6.1 5.6 6.7 5.6 5.8 6.2 5.6 5.9 6.1
#>  [73] 6.3 6.1 6.4 6.6 6.8 6.7 6.0 5.7 5.5 5.5 5.8 6.0 5.4 6.0 6.7 6.3 5.6 5.5
#>  [91] 5.5 6.1 5.8 5.0 5.6 5.7 5.7 6.2 5.1 5.7 6.3 5.8 7.1 6.3 6.5 7.6 4.9 7.3
#> [109] 6.7 7.2 6.5 6.4 6.8 5.7 5.8 6.4 6.5 7.7 7.7 6.0 6.9 5.6 7.7 6.3 6.7 7.2
#> [127] 6.2 6.1 6.4 7.2 7.4 7.9 6.4 6.3 6.1 7.7 6.3 6.4 6.0 6.9 6.7 6.9 5.8 6.8
#> [145] 6.7 6.7 6.3 6.5 6.2 5.9

Knowing this, you can use the mean() and IQR() functions.

mean(iris$Sepal.Length, na.rm = T)
#> [1] 5.843333
IQR(iris$Sepal.Length, na.rm = T)
#> [1] 1.3

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.