Get help with your R and Statistics assignments.
Call the tidyverse package -
> library(tidyverse)
Using the built in sample dataset CO2. Let’s look at the data set -
> glimpse(CO2)
Rows: 84
Columns: 5
$ Plant <ord> Qn1, Qn1, Qn1, Qn1, Qn1, Qn1, Qn1, Qn2, Qn2, Qn2, Qn2, Qn...
$ Type <fct> Quebec, Quebec, Quebec, Quebec, Quebec, Quebec, Quebec, Q...
$ Treatment <fct> nonchilled, nonchilled, nonchilled, nonchilled, nonchille...
$ conc <dbl> 95, 175, 250, 350, 500, 675, 1000, 95, 175, 250, 350, 500...
$ uptake <dbl> 16.0, 30.4, 34.8, 37.2, 35.3, 39.2, 39.7, 13.6, 27.3, 37....
Turn the data set into a tibble -
> CO2_tibble <- as_tibble(CO2)
> print(CO2_tibble)
# A tibble: 84 x 5
Plant Type Treatment conc uptake
<ord> <fct> <fct> <dbl> <dbl>
1 Qn1 Quebec nonchilled 95 16
2 Qn1 Quebec nonchilled 175 30.4
3 Qn1 Quebec nonchilled 250 34.8
4 Qn1 Quebec nonchilled 350 37.2
5 Qn1 Quebec nonchilled 500 35.3
6 Qn1 Quebec nonchilled 675 39.2
7 Qn1 Quebec nonchilled 1000 39.7
8 Qn2 Quebec nonchilled 95 13.6
9 Qn2 Quebec nonchilled 175 27.3
10 Qn2 Quebec nonchilled 250 37.1
# ... with 74 more rows
> print(CO2_tibble, n = 20) #shows first 20 rows
# A tibble: 84 x 5
Plant Type Treatment conc uptake
<ord> <fct> <fct> <dbl> <dbl>
1 Qn1 Quebec nonchilled 95 16
2 Qn1 Quebec nonchilled 175 30.4
3 Qn1 Quebec nonchilled 250 34.8
4 Qn1 Quebec nonchilled 350 37.2
5 Qn1 Quebec nonchilled 500 35.3
6 Qn1 Quebec nonchilled 675 39.2
7 Qn1 Quebec nonchilled 1000 39.7
8 Qn2 Quebec nonchilled 95 13.6
9 Qn2 Quebec nonchilled 175 27.3
10 Qn2 Quebec nonchilled 250 37.1
11 Qn2 Quebec nonchilled 350 41.8
12 Qn2 Quebec nonchilled 500 40.6
13 Qn2 Quebec nonchilled 675 41.4
14 Qn2 Quebec nonchilled 1000 44.3
15 Qn3 Quebec nonchilled 95 16.2
16 Qn3 Quebec nonchilled 175 32.4
17 Qn3 Quebec nonchilled 250 40.3
18 Qn3 Quebec nonchilled 350 42.1
19 Qn3 Quebec nonchilled 500 42.9
20 Qn3 Quebec nonchilled 675 43.9
# ... with 64 more rows
Use n=Inf in print() to show all the observations or use View(CO2_tibble)
Tibbles can be created in the same way as data frames -
> Name <- c("Sayem","Himel","Liuza","Ataur","Mohima","Joyes","Liuza","Champa")
> Gender <- as.factor(c("M","M","F","M","F","M","F","F"))
> Marks <- runif(8,40,100) %>% round() %>% as.integer()
> Stud <- tibble(Name, Gender, Marks)
> Stud
# A tibble: 8 x 3
Name Gender Marks
<chr> <fct> <int>
1 Sayem M 86
2 Himel M 82
3 Liuza F 96
4 Ataur M 45
5 Mohima F 89
6 Joyes M 43
7 Liuza F 93
8 Champa F 67
So the tibble is created.
Using $
From our created data set -
> Stud$Name
[1] "Sayem" "Himel" "Liuza" "Ataur" "Mohima" "Joyes" "Liuza" "Champa"
Using [[]]
> Stud[["Name"]]
[1] "Sayem" "Himel" "Liuza" "Ataur" "Mohima" "Joyes" "Liuza" "Champa"
Both returns the same result in vector form.
Using select (returns a data frame/tibble)
> select(Stud, Name)
# A tibble: 8 x 1
Name
<chr>
1 Sayem
2 Himel
3 Liuza
4 Ataur
5 Mohima
6 Joyes
7 Liuza
8 Champa
Using filter() -
> # Filtering students having marks more than 80
> filter(Stud, Marks > 80)
# A tibble: 5 x 3
Name Gender Marks
<chr> <fct> <int>
1 Sayem M 86
2 Himel M 82
3 Liuza F 96
4 Mohima F 89
5 Liuza F 93
> # Filtering male students
> filter(Stud, Gender == "M")
# A tibble: 4 x 3
Name Gender Marks
<chr> <fct> <int>
1 Sayem M 86
2 Himel M 82
3 Ataur M 45
4 Joyes M 43
> # Multiple filtering using comma
> filter(Stud, Gender == "M", Marks > 80)
# A tibble: 2 x 3
Name Gender Marks
<chr> <fct> <int>
1 Sayem M 86
2 Himel M 82