Primary exercises
In the survey data:
- Take a look at the table with
glimpse
and inspect its dimensions and types.
survey %>% glimpse()
Rows: 233
Columns: 13
$ name <chr> "Alyson", "Todd", "Gerald", "Robert", "Dustin", "Abby", "Andre", "Michael", "Edward", "Carl", "Noemi", "Alfred", "Bernice", "Velma", "Eddie", "Fern",…
$ gender <chr> "female", "male", "male", "male", "male", "female", "male", "female", "male", "male", "female", "male", "female", "female", "male", "female", "female…
$ span1 <dbl> 18.5, 19.5, 18.0, 18.8, 20.0, 18.0, 17.7, 17.0, 20.0, 18.5, 17.0, 21.0, 16.0, 19.5, 16.0, 17.5, 18.0, 19.4, 20.5, 21.0, 21.5, 20.1, 18.5, 21.5, 17.0,…
$ span2 <dbl> 18.0, 20.5, 13.3, 18.9, 20.0, 17.7, 17.7, 17.3, 19.5, 18.5, 17.2, 21.0, 16.0, 20.2, 15.5, 17.0, 18.0, 19.2, 20.5, 20.9, 22.0, 20.7, 18.0, 21.2, 17.5,…
$ hand <chr> "right", "left", "right", "right", "right", "right", "right", "right", "right", "right", "right", "right", "right", "right", "right", "right", "right…
$ fold <chr> "right", "right", "left", "right", "neither", "left", "left", "right", "right", "right", "left", "right", "left", "left", "right", "right", "left", "…
$ pulse <dbl> 92, 104, 87, NA, 35, 64, 83, 74, 72, 90, 80, 68, NA, 66, 60, NA, 89, 74, NA, 78, 72, 72, 64, 62, 64, 90, 90, 62, 76, 79, 76, 78, 72, 70, 54, 66, NA, …
$ clap <chr> "left", "left", "neither", "neither", "right", "right", "right", "right", "right", "right", "right", "left", "right", "neither", "right", "right", "n…
$ exercise <chr> "some", "none", "none", "none", "some", "some", "freq", "freq", "some", "some", "freq", "freq", "some", "some", "some", "freq", "freq", "some", "some…
$ smokes <chr> "never", "regul", "occas", "never", "never", "never", "never", "never", "never", "never", "never", "never", "never", "never", "never", "never", "neve…
$ height <dbl> 173.00, 177.80, NA, 160.00, 165.00, 172.72, 182.88, 157.00, 175.00, 167.00, 156.20, NA, 155.00, 155.00, NA, 156.00, 157.00, 182.88, 190.50, 177.00, 1…
$ m.i <chr> "metric", "imperial", NA, "metric", "metric", "imperial", "imperial", "metric", "metric", "metric", "imperial", NA, "metric", "metric", NA, "metric",…
$ age <dbl> 18.250, 17.583, 16.917, 20.333, 23.667, 21.000, 18.833, 35.833, 19.000, 22.333, 28.500, 18.250, 18.750, 17.500, 17.167, 17.167, 19.333, 18.333, 19.75…
- Summarise on mean hand spans
{span1,span2}
and medianpulse
.
# 'pulse' variable has missing values (NA), therefore the use
# of 'na.rm=TRUE' in the function 'median'.
#
survey %>% summarise(meanWritingHand=mean(span1),
meanNonWritingHand=mean(span2),
medianPulse=median(pulse, na.rm=TRUE))
# A tibble: 1 × 3
meanWritingHand meanNonWritingHand medianPulse
<dbl> <dbl> <dbl>
1 18.7 18.6 73
- Summarise on mean
age
and medianheight
.
survey %>% summarise(meanAge=mean(age), medianHeight=median(height, na.rm=TRUE))
# A tibble: 1 × 2
meanAge medianHeight
<dbl> <dbl>
1 20.4 171
- Count the number of males and females.
survey %>% count(gender)
# A tibble: 2 × 2
gender n
<chr> <int>
1 female 117
2 male 116
- Produce the frequency tables on {
gender
,fold
} and {gender
,fold
,clap
}.
survey %>% count(gender, fold)
# A tibble: 6 × 3
gender fold n
<chr> <chr> <int>
1 female left 47
2 female neither 6
3 female right 64
4 male left 49
5 male neither 12
6 male right 55
survey %>% count(gender, fold, clap)
# A tibble: 15 × 4
gender fold clap n
<chr> <chr> <chr> <int>
1 female left left 8
2 female left neither 8
3 female left right 31
4 female neither right 6
5 female right left 12
6 female right neither 16
7 female right right 36
8 male left left 7
9 male left neither 13
10 male left right 29
11 male neither neither 2
12 male neither right 10
13 male right left 10
14 male right neither 10
15 male right right 35
- Produce the frequency table on
gender
andsmokes
, show only females.
survey %>% count(gender,smokes) %>% filter(gender=="female")
# A tibble: 4 × 3
gender smokes n
<chr> <chr> <int>
1 female heavy 5
2 female never 98
3 female occas 9
4 female regul 5
- Summarise median age of male heavy smokers. Do the same for females.
# male
survey %>% filter(gender=='male' & smokes=='heavy') %>% summarise(median_age=median(age))
# A tibble: 1 × 1
median_age
<dbl>
1 20.5
# female
survey %>% filter(gender=='female' & smokes=='heavy') %>% summarise(median_age=median(age))
# A tibble: 1 × 1
median_age
<dbl>
1 18.4