Primary exercises
Apply the following to survey data:
- Select personal information {name, age, gender, height} into a new tibble
survey_personal_info
.
survey_personal_info <- select(survey, name, age, gender, height)
- Select personal information as previous exercise into a new tibble
survey_personal_info
but with variable names initials in uppercase, e.g.Name
,Age
etc.
survey_personal_info <- select(survey, Name=name, Age=age, Gender=gender, Height=height)
- Reorder the variables in survey dataset as such that name,age and gender appear as first, second and the third column followed by the remaining variables.
select(survey, name,age,gender,everything())
# A tibble: 233 × 13
name age gender span1 span2 hand fold pulse clap exercise smokes height m.i
<chr> <dbl> <chr> <dbl> <dbl> <chr> <chr> <dbl> <chr> <chr> <chr> <dbl> <chr>
1 Alyson 18.2 female 18.5 18 right right 92 left some never 173 metric
2 Todd 17.6 male 19.5 20.5 left right 104 left none regul 178. imperial
3 Gerald 16.9 male 18 13.3 right left 87 neither none occas NA <NA>
4 Robert 20.3 male 18.8 18.9 right right NA neither none never 160 metric
5 Dustin 23.7 male 20 20 right neither 35 right some never 165 metric
6 Abby 21 female 18 17.7 right left 64 right some never 173. imperial
7 Andre 18.8 male 17.7 17.7 right left 83 right freq never 183. imperial
8 Michael 35.8 female 17 17.3 right right 74 right freq never 157 metric
9 Edward 19 male 20 19.5 right right 72 right some never 175 metric
10 Carl 22.3 male 18.5 18.5 right right 90 right some never 167 metric
# … with 223 more rows
- Deselect variables that relate to hand and/or arm (e.g. span1, span2, hand, etc.). See also description survey data.
select(survey, -span1,-span2,-hand,-fold,-clap)
# A tibble: 233 × 8
name gender pulse exercise smokes height m.i age
<chr> <chr> <dbl> <chr> <chr> <dbl> <chr> <dbl>
1 Alyson female 92 some never 173 metric 18.2
2 Todd male 104 none regul 178. imperial 17.6
3 Gerald male 87 none occas NA <NA> 16.9
4 Robert male NA none never 160 metric 20.3
5 Dustin male 35 some never 165 metric 23.7
6 Abby female 64 some never 173. imperial 21
7 Andre male 83 freq never 183. imperial 18.8
8 Michael female 74 freq never 157 metric 35.8
9 Edward male 72 some never 175 metric 19
10 Carl male 90 some never 167 metric 22.3
# … with 223 more rows
- Select the top 20 names along with gender.
# 1)
survey_sub <- select(survey, name,gender)
head( survey_sub , 20)
# A tibble: 20 × 2
name gender
<chr> <chr>
1 Alyson female
2 Todd male
3 Gerald male
4 Robert male
5 Dustin male
6 Abby female
7 Andre male
8 Michael female
9 Edward male
10 Carl male
11 Noemi female
12 Alfred male
13 Bernice female
14 Velma female
15 Eddie male
16 Fern female
17 Carolyn female
18 Virgil male
19 Ken male
20 Richard male
# 2) shorter solution without intermediate variable 'survey_sub' :
head(select(survey,name),20)
# A tibble: 20 × 1
name
<chr>
1 Alyson
2 Todd
3 Gerald
4 Robert
5 Dustin
6 Abby
7 Andre
8 Michael
9 Edward
10 Carl
11 Noemi
12 Alfred
13 Bernice
14 Velma
15 Eddie
16 Fern
17 Carolyn
18 Virgil
19 Ken
20 Richard
Reproduce the following tibbles (note that variables are renamed and reshuffled):
6.1 First 5 observations.
# Remark: by enclosing select(...) as the first argument of 'head' function you # can avoid creating intermediate variables. head( select(survey, SPAN1=span1, SPAN2=span2, everything()), 5)
# A tibble: 5 × 13 SPAN1 SPAN2 name gender hand fold pulse clap exercise smokes height m.i age <dbl> <dbl> <chr> <chr> <chr> <chr> <dbl> <chr> <chr> <chr> <dbl> <chr> <dbl> 1 18.5 18 Alyson female right right 92 left some never 173 metric 18.2 2 19.5 20.5 Todd male left right 104 left none regul 178. imperial 17.6 3 18 13.3 Gerald male right left 87 neither none occas NA <NA> 16.9 4 18.8 18.9 Robert male right right NA neither none never 160 metric 20.3 5 20 20 Dustin male right neither 35 right some never 165 metric 23.7
6.1 Last 3 observations.
tail( select(survey, Hand=hand,Fold=fold,Clap=clap, everything()), 3)
# A tibble: 3 × 13 Hand Fold Clap name gender span1 span2 pulse exercise smokes height m.i age <chr> <chr> <chr> <chr> <chr> <dbl> <dbl> <dbl> <chr> <chr> <dbl> <chr> <dbl> 1 right right right Tracey female 17.5 16.5 NA some never 170 metric 18.6 2 right right right Keith male 21 21.5 90 some never 183 metric 17.2 3 right right right Celina female 17.6 17.3 85 freq never 168. metric 17.8