Cereal.csv data file from Canvas for this
lab.R
markdown file, run some simple code. The questions here aim to get you
familiar with the R syntax.Before you start the tutorial, create a new RStudio project in a new directory. Then, start your tutorial by writing your answers in a new R Markdown file.
Cereal.csv file from the Canvas page and
use the read.csv command to read in the csv file into
R and assign it to the object called
cereal.library(readr)
library(dplyr)
##
## 載入套件:'dplyr'
## 下列物件被遮斷自 'package:stats':
##
## filter, lag
## 下列物件被遮斷自 'package:base':
##
## intersect, setdiff, setequal, union
library(ggplot2)
cereal <- read.csv("D:/OneDrive - The University of Sydney (Students)/Desktop/5003/Cereal.csv", header = TRUE)
cereal_df <- read.csv("D:/OneDrive - The University of Sydney (Students)/Desktop/5003/Cereal.csv", header = TRUE)
cereal_tb1 <- read.csv("D:/OneDrive - The University of Sydney (Students)/Desktop/5003/Cereal.csv")
R called
cereal. Use the head function to inspect the
first few lines of the data frame and use class to check
that cereal is in fact a data frame.cereal_tb1 %>% head(7)
## name mfr type calories protein fat sodium fiber carbo
## 1 100%_Bran N C 70 4 1 130 10.0 5.0
## 2 100%_Natural_Bran Q C 120 3 5 15 2.0 8.0
## 3 All-Bran K C 70 4 1 260 9.0 7.0
## 4 All-Bran_with_Extra_Fiber K C 50 4 0 140 14.0 8.0
## 5 Almond_Delight R C 110 2 2 200 1.0 14.0
## 6 Apple_Cinnamon_Cheerios G C 110 2 2 180 1.5 10.5
## 7 Apple_Jacks K C 110 2 0 125 1.0 11.0
## sugars potass vitamins shelf weight cups rating
## 1 6 280 25 3 1 0.33 68.40297
## 2 8 135 0 3 1 1.00 33.98368
## 3 5 320 25 3 1 0.33 59.42551
## 4 0 330 25 3 1 0.50 93.70491
## 5 8 -1 25 3 1 0.75 34.38484
## 6 10 70 25 1 1 0.75 29.50954
## 7 14 30 25 2 1 1.00 33.17409
cereal_tb1 %>% class()
## [1] "data.frame"
cereal data frame? How
many rows are there? (dim and nrow)cereal_tb1 %>% colnames()
## [1] "name" "mfr" "type" "calories" "protein" "fat"
## [7] "sodium" "fiber" "carbo" "sugars" "potass" "vitamins"
## [13] "shelf" "weight" "cups" "rating"
cereal_tb1 %>% dim()
## [1] 77 16
cereal_tb1 %>% nrow()
## [1] 77
calories column using the $
operator and using the [[ operator.cal <- cereal_tb1 %>% select(calories)
cal <- cereal_tb1 %>% pull(calories)
class(cereal_tb1[["calories"]])
## [1] "integer"
cereal data frame.cereal_tb1 %>% slice(1:10)
## name mfr type calories protein fat sodium fiber carbo
## 1 100%_Bran N C 70 4 1 130 10.0 5.0
## 2 100%_Natural_Bran Q C 120 3 5 15 2.0 8.0
## 3 All-Bran K C 70 4 1 260 9.0 7.0
## 4 All-Bran_with_Extra_Fiber K C 50 4 0 140 14.0 8.0
## 5 Almond_Delight R C 110 2 2 200 1.0 14.0
## 6 Apple_Cinnamon_Cheerios G C 110 2 2 180 1.5 10.5
## 7 Apple_Jacks K C 110 2 0 125 1.0 11.0
## 8 Basic_4 G C 130 3 2 210 2.0 18.0
## 9 Bran_Chex R C 90 2 1 200 4.0 15.0
## 10 Bran_Flakes P C 90 3 0 210 5.0 13.0
## sugars potass vitamins shelf weight cups rating
## 1 6 280 25 3 1.00 0.33 68.40297
## 2 8 135 0 3 1.00 1.00 33.98368
## 3 5 320 25 3 1.00 0.33 59.42551
## 4 0 330 25 3 1.00 0.50 93.70491
## 5 8 -1 25 3 1.00 0.75 34.38484
## 6 10 70 25 1 1.00 0.75 29.50954
## 7 14 30 25 2 1.00 1.00 33.17409
## 8 8 100 25 3 1.33 0.75 37.03856
## 9 6 125 25 1 1.00 0.67 49.12025
## 10 5 190 25 3 1.00 0.67 53.31381
Kelloggs which only
contains rows that belongs to manufacturer, Kellogs (when
mfr takes the value "K").kelloogs <- cereal_tb1 %>% filter(mfr == "K")
Cereal data again with the
read.csv command again. This time, use the optional
argument, stringsAsFactors = TRUE.cereal <- read.csv("D:/OneDrive - The University of Sydney (Students)/Desktop/5003/Cereal.csv")
cereal.with.factors <- read.csv("D:/OneDrive - The University of Sydney (Students)/Desktop/5003/Cereal.csv", stringsAsFactors = TRUE)
cereal.with.factors
## name mfr type calories protein fat sodium
## 1 100%_Bran N C 70 4 1 130
## 2 100%_Natural_Bran Q C 120 3 5 15
## 3 All-Bran K C 70 4 1 260
## 4 All-Bran_with_Extra_Fiber K C 50 4 0 140
## 5 Almond_Delight R C 110 2 2 200
## 6 Apple_Cinnamon_Cheerios G C 110 2 2 180
## 7 Apple_Jacks K C 110 2 0 125
## 8 Basic_4 G C 130 3 2 210
## 9 Bran_Chex R C 90 2 1 200
## 10 Bran_Flakes P C 90 3 0 210
## 11 Cap'n'Crunch Q C 120 1 2 220
## 12 Cheerios G C 110 6 2 290
## 13 Cinnamon_Toast_Crunch G C 120 1 3 210
## 14 Clusters G C 110 3 2 140
## 15 Cocoa_Puffs G C 110 1 1 180
## 16 Corn_Chex R C 110 2 0 280
## 17 Corn_Flakes K C 100 2 0 290
## 18 Corn_Pops K C 110 1 0 90
## 19 Count_Chocula G C 110 1 1 180
## 20 Cracklin'_Oat_Bran K C 110 3 3 140
## 21 Cream_of_Wheat_(Quick) N H 100 3 0 80
## 22 Crispix K C 110 2 0 220
## 23 Crispy_Wheat_&_Raisins G C 100 2 1 140
## 24 Double_Chex R C 100 2 0 190
## 25 Froot_Loops K C 110 2 1 125
## 26 Frosted_Flakes K C 110 1 0 200
## 27 Frosted_Mini-Wheats K C 100 3 0 0
## 28 Fruit_&_Fibre_Dates,_Walnuts,_and_Oats P C 120 3 2 160
## 29 Fruitful_Bran K C 120 3 0 240
## 30 Fruity_Pebbles P C 110 1 1 135
## 31 Golden_Crisp P C 100 2 0 45
## 32 Golden_Grahams G C 110 1 1 280
## 33 Grape_Nuts_Flakes P C 100 3 1 140
## 34 Grape-Nuts P C 110 3 0 170
## 35 Great_Grains_Pecan P C 120 3 3 75
## 36 Honey_Graham_Ohs Q C 120 1 2 220
## 37 Honey_Nut_Cheerios G C 110 3 1 250
## 38 Honey-comb P C 110 1 0 180
## 39 Just_Right_Crunchy__Nuggets K C 110 2 1 170
## 40 Just_Right_Fruit_&_Nut K C 140 3 1 170
## 41 Kix G C 110 2 1 260
## 42 Life Q C 100 4 2 150
## 43 Lucky_Charms G C 110 2 1 180
## 44 Maypo A H 100 4 1 0
## 45 Muesli_Raisins,_Dates,_&_Almonds R C 150 4 3 95
## 46 Muesli_Raisins,_Peaches,_&_Pecans R C 150 4 3 150
## 47 Mueslix_Crispy_Blend K C 160 3 2 150
## 48 Multi-Grain_Cheerios G C 100 2 1 220
## 49 Nut&Honey_Crunch K C 120 2 1 190
## 50 Nutri-Grain_Almond-Raisin K C 140 3 2 220
## 51 Nutri-grain_Wheat K C 90 3 0 170
## 52 Oatmeal_Raisin_Crisp G C 130 3 2 170
## 53 Post_Nat._Raisin_Bran P C 120 3 1 200
## 54 Product_19 K C 100 3 0 320
## 55 Puffed_Rice Q C 50 1 0 0
## 56 Puffed_Wheat Q C 50 2 0 0
## 57 Quaker_Oat_Squares Q C 100 4 1 135
## 58 Quaker_Oatmeal Q H 100 5 2 0
## 59 Raisin_Bran K C 120 3 1 210
## 60 Raisin_Nut_Bran G C 100 3 2 140
## 61 Raisin_Squares K C 90 2 0 0
## 62 Rice_Chex R C 110 1 0 240
## 63 Rice_Krispies K C 110 2 0 290
## 64 Shredded_Wheat N C 80 2 0 0
## 65 Shredded_Wheat_'n'Bran N C 90 3 0 0
## 66 Shredded_Wheat_spoon_size N C 90 3 0 0
## 67 Smacks K C 110 2 1 70
## 68 Special_K K C 110 6 0 230
## 69 Strawberry_Fruit_Wheats N C 90 2 0 15
## 70 Total_Corn_Flakes G C 110 2 1 200
## 71 Total_Raisin_Bran G C 140 3 1 190
## 72 Total_Whole_Grain G C 100 3 1 200
## 73 Triples G C 110 2 1 250
## 74 Trix G C 110 1 1 140
## 75 Wheat_Chex R C 100 3 1 230
## 76 Wheaties G C 100 3 1 200
## 77 Wheaties_Honey_Gold G C 110 2 1 200
## fiber carbo sugars potass vitamins shelf weight cups rating
## 1 10.0 5.0 6 280 25 3 1.00 0.33 68.40297
## 2 2.0 8.0 8 135 0 3 1.00 1.00 33.98368
## 3 9.0 7.0 5 320 25 3 1.00 0.33 59.42551
## 4 14.0 8.0 0 330 25 3 1.00 0.50 93.70491
## 5 1.0 14.0 8 -1 25 3 1.00 0.75 34.38484
## 6 1.5 10.5 10 70 25 1 1.00 0.75 29.50954
## 7 1.0 11.0 14 30 25 2 1.00 1.00 33.17409
## 8 2.0 18.0 8 100 25 3 1.33 0.75 37.03856
## 9 4.0 15.0 6 125 25 1 1.00 0.67 49.12025
## 10 5.0 13.0 5 190 25 3 1.00 0.67 53.31381
## 11 0.0 12.0 12 35 25 2 1.00 0.75 18.04285
## 12 2.0 17.0 1 105 25 1 1.00 1.25 50.76500
## 13 0.0 13.0 9 45 25 2 1.00 0.75 19.82357
## 14 2.0 13.0 7 105 25 3 1.00 0.50 40.40021
## 15 0.0 12.0 13 55 25 2 1.00 1.00 22.73645
## 16 0.0 22.0 3 25 25 1 1.00 1.00 41.44502
## 17 1.0 21.0 2 35 25 1 1.00 1.00 45.86332
## 18 1.0 13.0 12 20 25 2 1.00 1.00 35.78279
## 19 0.0 12.0 13 65 25 2 1.00 1.00 22.39651
## 20 4.0 10.0 7 160 25 3 1.00 0.50 40.44877
## 21 1.0 21.0 0 -1 0 2 1.00 1.00 64.53382
## 22 1.0 21.0 3 30 25 3 1.00 1.00 46.89564
## 23 2.0 11.0 10 120 25 3 1.00 0.75 36.17620
## 24 1.0 18.0 5 80 25 3 1.00 0.75 44.33086
## 25 1.0 11.0 13 30 25 2 1.00 1.00 32.20758
## 26 1.0 14.0 11 25 25 1 1.00 0.75 31.43597
## 27 3.0 14.0 7 100 25 2 1.00 0.80 58.34514
## 28 5.0 12.0 10 200 25 3 1.25 0.67 40.91705
## 29 5.0 14.0 12 190 25 3 1.33 0.67 41.01549
## 30 0.0 13.0 12 25 25 2 1.00 0.75 28.02576
## 31 0.0 11.0 15 40 25 1 1.00 0.88 35.25244
## 32 0.0 15.0 9 45 25 2 1.00 0.75 23.80404
## 33 3.0 15.0 5 85 25 3 1.00 0.88 52.07690
## 34 3.0 17.0 3 90 25 3 1.00 0.25 53.37101
## 35 3.0 13.0 4 100 25 3 1.00 0.33 45.81172
## 36 1.0 12.0 11 45 25 2 1.00 1.00 21.87129
## 37 1.5 11.5 10 90 25 1 1.00 0.75 31.07222
## 38 0.0 14.0 11 35 25 1 1.00 1.33 28.74241
## 39 1.0 17.0 6 60 100 3 1.00 1.00 36.52368
## 40 2.0 20.0 9 95 100 3 1.30 0.75 36.47151
## 41 0.0 21.0 3 40 25 2 1.00 1.50 39.24111
## 42 2.0 12.0 6 95 25 2 1.00 0.67 45.32807
## 43 0.0 12.0 12 55 25 2 1.00 1.00 26.73451
## 44 0.0 16.0 3 95 25 2 1.00 1.00 54.85092
## 45 3.0 16.0 11 170 25 3 1.00 1.00 37.13686
## 46 3.0 16.0 11 170 25 3 1.00 1.00 34.13976
## 47 3.0 17.0 13 160 25 3 1.50 0.67 30.31335
## 48 2.0 15.0 6 90 25 1 1.00 1.00 40.10596
## 49 0.0 15.0 9 40 25 2 1.00 0.67 29.92429
## 50 3.0 21.0 7 130 25 3 1.33 0.67 40.69232
## 51 3.0 18.0 2 90 25 3 1.00 1.00 59.64284
## 52 1.5 13.5 10 120 25 3 1.25 0.50 30.45084
## 53 6.0 11.0 14 260 25 3 1.33 0.67 37.84059
## 54 1.0 20.0 3 45 100 3 1.00 1.00 41.50354
## 55 0.0 13.0 0 15 0 3 0.50 1.00 60.75611
## 56 1.0 10.0 0 50 0 3 0.50 1.00 63.00565
## 57 2.0 14.0 6 110 25 3 1.00 0.50 49.51187
## 58 2.7 -1.0 -1 110 0 1 1.00 0.67 50.82839
## 59 5.0 14.0 12 240 25 2 1.33 0.75 39.25920
## 60 2.5 10.5 8 140 25 3 1.00 0.50 39.70340
## 61 2.0 15.0 6 110 25 3 1.00 0.50 55.33314
## 62 0.0 23.0 2 30 25 1 1.00 1.13 41.99893
## 63 0.0 22.0 3 35 25 1 1.00 1.00 40.56016
## 64 3.0 16.0 0 95 0 1 0.83 1.00 68.23588
## 65 4.0 19.0 0 140 0 1 1.00 0.67 74.47295
## 66 3.0 20.0 0 120 0 1 1.00 0.67 72.80179
## 67 1.0 9.0 15 40 25 2 1.00 0.75 31.23005
## 68 1.0 16.0 3 55 25 1 1.00 1.00 53.13132
## 69 3.0 15.0 5 90 25 2 1.00 1.00 59.36399
## 70 0.0 21.0 3 35 100 3 1.00 1.00 38.83975
## 71 4.0 15.0 14 230 100 3 1.50 1.00 28.59278
## 72 3.0 16.0 3 110 100 3 1.00 1.00 46.65884
## 73 0.0 21.0 3 60 25 3 1.00 0.75 39.10617
## 74 0.0 13.0 12 25 25 2 1.00 1.00 27.75330
## 75 3.0 17.0 3 115 25 1 1.00 0.67 49.78744
## 76 3.0 17.0 3 110 25 1 1.00 1.00 51.59219
## 77 1.0 16.0 8 60 25 1 1.00 0.75 36.18756
mfr and type columns are now factors.
Check that this is true.levels(cereal.with.factors$mfr)
## [1] "A" "G" "K" "N" "P" "Q" "R"
class(cereal.with.factors$mfr)
## [1] "factor"
class(cereal$mfr)
## [1] "character"
class(cereal.with.factors$carbo)
## [1] "numeric"
class(cereal$carbo)
## [1] "numeric"
str(cereal.with.factors)
## 'data.frame': 77 obs. of 16 variables:
## $ name : Factor w/ 77 levels "100%_Bran","100%_Natural_Bran",..: 1 2 3 4 5 6 7 8 9 10 ...
## $ mfr : Factor w/ 7 levels "A","G","K","N",..: 4 6 3 3 7 2 3 2 7 5 ...
## $ type : Factor w/ 2 levels "C","H": 1 1 1 1 1 1 1 1 1 1 ...
## $ calories: int 70 120 70 50 110 110 110 130 90 90 ...
## $ protein : int 4 3 4 4 2 2 2 3 2 3 ...
## $ fat : int 1 5 1 0 2 2 0 2 1 0 ...
## $ sodium : int 130 15 260 140 200 180 125 210 200 210 ...
## $ fiber : num 10 2 9 14 1 1.5 1 2 4 5 ...
## $ carbo : num 5 8 7 8 14 10.5 11 18 15 13 ...
## $ sugars : int 6 8 5 0 8 10 14 8 6 5 ...
## $ potass : int 280 135 320 330 -1 70 30 100 125 190 ...
## $ vitamins: int 25 0 25 25 25 25 25 25 25 25 ...
## $ shelf : int 3 3 3 3 3 1 2 3 1 3 ...
## $ weight : num 1 1 1 1 1 1 1 1.33 1 1 ...
## $ cups : num 0.33 1 0.33 0.5 0.75 0.75 1 0.75 0.67 0.67 ...
## $ rating : num 68.4 34 59.4 93.7 34.4 ...
str(cereal)
## 'data.frame': 77 obs. of 16 variables:
## $ name : chr "100%_Bran" "100%_Natural_Bran" "All-Bran" "All-Bran_with_Extra_Fiber" ...
## $ mfr : chr "N" "Q" "K" "K" ...
## $ type : chr "C" "C" "C" "C" ...
## $ calories: int 70 120 70 50 110 110 110 130 90 90 ...
## $ protein : int 4 3 4 4 2 2 2 3 2 3 ...
## $ fat : int 1 5 1 0 2 2 0 2 1 0 ...
## $ sodium : int 130 15 260 140 200 180 125 210 200 210 ...
## $ fiber : num 10 2 9 14 1 1.5 1 2 4 5 ...
## $ carbo : num 5 8 7 8 14 10.5 11 18 15 13 ...
## $ sugars : int 6 8 5 0 8 10 14 8 6 5 ...
## $ potass : int 280 135 320 330 -1 70 30 100 125 190 ...
## $ vitamins: int 25 0 25 25 25 25 25 25 25 25 ...
## $ shelf : int 3 3 3 3 3 1 2 3 1 3 ...
## $ weight : num 1 1 1 1 1 1 1 1.33 1 1 ...
## $ cups : num 0.33 1 0.33 0.5 0.75 0.75 1 0.75 0.67 0.67 ...
## $ rating : num 68.4 34 59.4 93.7 34.4 ...
mfr and type?
(use the functions levels or nlevels)levels(cereal.with.factors$mfr)
## [1] "A" "G" "K" "N" "P" "Q" "R"
nlevels(cereal.with.factors$mfr)
## [1] 7
str(cereal.with.factors$mfr)
## Factor w/ 7 levels "A","G","K","N",..: 4 6 3 3 7 2 3 2 7 5 ...
calories into a new vector called
cereal.calories.cereal.calories <- cereal_tb1 %>% pull(calories)
cereal.calories?
(length)length(cereal.calories)
## [1] 77
cereal.calories.cereal.calories[5:10]
## [1] 110 110 110 130 90 90
cereal.calories using
c().cereal.calories <- c(cereal.calories,1.0)
cereal.calories %>% length()
## [1] 78
as.matrix(cereal)). Check that the elements have been
forced into the character type.cereal.matrix <- as.matrix(cereal)
str(cereal.matrix)
## chr [1:77, 1:16] "100%_Bran" "100%_Natural_Bran" "All-Bran" ...
## - attr(*, "dimnames")=List of 2
## ..$ : NULL
## ..$ : chr [1:16] "name" "mfr" "type" "calories" ...
mfr,
name and type columns. Check that the elements
are now numeric.cereal_tb1.removed <- cereal_tb1[,-(1:3)]
cereal_tb1.removed
## calories protein fat sodium fiber carbo sugars potass vitamins shelf weight
## 1 70 4 1 130 10.0 5.0 6 280 25 3 1.00
## 2 120 3 5 15 2.0 8.0 8 135 0 3 1.00
## 3 70 4 1 260 9.0 7.0 5 320 25 3 1.00
## 4 50 4 0 140 14.0 8.0 0 330 25 3 1.00
## 5 110 2 2 200 1.0 14.0 8 -1 25 3 1.00
## 6 110 2 2 180 1.5 10.5 10 70 25 1 1.00
## 7 110 2 0 125 1.0 11.0 14 30 25 2 1.00
## 8 130 3 2 210 2.0 18.0 8 100 25 3 1.33
## 9 90 2 1 200 4.0 15.0 6 125 25 1 1.00
## 10 90 3 0 210 5.0 13.0 5 190 25 3 1.00
## 11 120 1 2 220 0.0 12.0 12 35 25 2 1.00
## 12 110 6 2 290 2.0 17.0 1 105 25 1 1.00
## 13 120 1 3 210 0.0 13.0 9 45 25 2 1.00
## 14 110 3 2 140 2.0 13.0 7 105 25 3 1.00
## 15 110 1 1 180 0.0 12.0 13 55 25 2 1.00
## 16 110 2 0 280 0.0 22.0 3 25 25 1 1.00
## 17 100 2 0 290 1.0 21.0 2 35 25 1 1.00
## 18 110 1 0 90 1.0 13.0 12 20 25 2 1.00
## 19 110 1 1 180 0.0 12.0 13 65 25 2 1.00
## 20 110 3 3 140 4.0 10.0 7 160 25 3 1.00
## 21 100 3 0 80 1.0 21.0 0 -1 0 2 1.00
## 22 110 2 0 220 1.0 21.0 3 30 25 3 1.00
## 23 100 2 1 140 2.0 11.0 10 120 25 3 1.00
## 24 100 2 0 190 1.0 18.0 5 80 25 3 1.00
## 25 110 2 1 125 1.0 11.0 13 30 25 2 1.00
## 26 110 1 0 200 1.0 14.0 11 25 25 1 1.00
## 27 100 3 0 0 3.0 14.0 7 100 25 2 1.00
## 28 120 3 2 160 5.0 12.0 10 200 25 3 1.25
## 29 120 3 0 240 5.0 14.0 12 190 25 3 1.33
## 30 110 1 1 135 0.0 13.0 12 25 25 2 1.00
## 31 100 2 0 45 0.0 11.0 15 40 25 1 1.00
## 32 110 1 1 280 0.0 15.0 9 45 25 2 1.00
## 33 100 3 1 140 3.0 15.0 5 85 25 3 1.00
## 34 110 3 0 170 3.0 17.0 3 90 25 3 1.00
## 35 120 3 3 75 3.0 13.0 4 100 25 3 1.00
## 36 120 1 2 220 1.0 12.0 11 45 25 2 1.00
## 37 110 3 1 250 1.5 11.5 10 90 25 1 1.00
## 38 110 1 0 180 0.0 14.0 11 35 25 1 1.00
## 39 110 2 1 170 1.0 17.0 6 60 100 3 1.00
## 40 140 3 1 170 2.0 20.0 9 95 100 3 1.30
## 41 110 2 1 260 0.0 21.0 3 40 25 2 1.00
## 42 100 4 2 150 2.0 12.0 6 95 25 2 1.00
## 43 110 2 1 180 0.0 12.0 12 55 25 2 1.00
## 44 100 4 1 0 0.0 16.0 3 95 25 2 1.00
## 45 150 4 3 95 3.0 16.0 11 170 25 3 1.00
## 46 150 4 3 150 3.0 16.0 11 170 25 3 1.00
## 47 160 3 2 150 3.0 17.0 13 160 25 3 1.50
## 48 100 2 1 220 2.0 15.0 6 90 25 1 1.00
## 49 120 2 1 190 0.0 15.0 9 40 25 2 1.00
## 50 140 3 2 220 3.0 21.0 7 130 25 3 1.33
## 51 90 3 0 170 3.0 18.0 2 90 25 3 1.00
## 52 130 3 2 170 1.5 13.5 10 120 25 3 1.25
## 53 120 3 1 200 6.0 11.0 14 260 25 3 1.33
## 54 100 3 0 320 1.0 20.0 3 45 100 3 1.00
## 55 50 1 0 0 0.0 13.0 0 15 0 3 0.50
## 56 50 2 0 0 1.0 10.0 0 50 0 3 0.50
## 57 100 4 1 135 2.0 14.0 6 110 25 3 1.00
## 58 100 5 2 0 2.7 -1.0 -1 110 0 1 1.00
## 59 120 3 1 210 5.0 14.0 12 240 25 2 1.33
## 60 100 3 2 140 2.5 10.5 8 140 25 3 1.00
## 61 90 2 0 0 2.0 15.0 6 110 25 3 1.00
## 62 110 1 0 240 0.0 23.0 2 30 25 1 1.00
## 63 110 2 0 290 0.0 22.0 3 35 25 1 1.00
## 64 80 2 0 0 3.0 16.0 0 95 0 1 0.83
## 65 90 3 0 0 4.0 19.0 0 140 0 1 1.00
## 66 90 3 0 0 3.0 20.0 0 120 0 1 1.00
## 67 110 2 1 70 1.0 9.0 15 40 25 2 1.00
## 68 110 6 0 230 1.0 16.0 3 55 25 1 1.00
## 69 90 2 0 15 3.0 15.0 5 90 25 2 1.00
## 70 110 2 1 200 0.0 21.0 3 35 100 3 1.00
## 71 140 3 1 190 4.0 15.0 14 230 100 3 1.50
## 72 100 3 1 200 3.0 16.0 3 110 100 3 1.00
## 73 110 2 1 250 0.0 21.0 3 60 25 3 1.00
## 74 110 1 1 140 0.0 13.0 12 25 25 2 1.00
## 75 100 3 1 230 3.0 17.0 3 115 25 1 1.00
## 76 100 3 1 200 3.0 17.0 3 110 25 1 1.00
## 77 110 2 1 200 1.0 16.0 8 60 25 1 1.00
## cups rating
## 1 0.33 68.40297
## 2 1.00 33.98368
## 3 0.33 59.42551
## 4 0.50 93.70491
## 5 0.75 34.38484
## 6 0.75 29.50954
## 7 1.00 33.17409
## 8 0.75 37.03856
## 9 0.67 49.12025
## 10 0.67 53.31381
## 11 0.75 18.04285
## 12 1.25 50.76500
## 13 0.75 19.82357
## 14 0.50 40.40021
## 15 1.00 22.73645
## 16 1.00 41.44502
## 17 1.00 45.86332
## 18 1.00 35.78279
## 19 1.00 22.39651
## 20 0.50 40.44877
## 21 1.00 64.53382
## 22 1.00 46.89564
## 23 0.75 36.17620
## 24 0.75 44.33086
## 25 1.00 32.20758
## 26 0.75 31.43597
## 27 0.80 58.34514
## 28 0.67 40.91705
## 29 0.67 41.01549
## 30 0.75 28.02576
## 31 0.88 35.25244
## 32 0.75 23.80404
## 33 0.88 52.07690
## 34 0.25 53.37101
## 35 0.33 45.81172
## 36 1.00 21.87129
## 37 0.75 31.07222
## 38 1.33 28.74241
## 39 1.00 36.52368
## 40 0.75 36.47151
## 41 1.50 39.24111
## 42 0.67 45.32807
## 43 1.00 26.73451
## 44 1.00 54.85092
## 45 1.00 37.13686
## 46 1.00 34.13976
## 47 0.67 30.31335
## 48 1.00 40.10596
## 49 0.67 29.92429
## 50 0.67 40.69232
## 51 1.00 59.64284
## 52 0.50 30.45084
## 53 0.67 37.84059
## 54 1.00 41.50354
## 55 1.00 60.75611
## 56 1.00 63.00565
## 57 0.50 49.51187
## 58 0.67 50.82839
## 59 0.75 39.25920
## 60 0.50 39.70340
## 61 0.50 55.33314
## 62 1.13 41.99893
## 63 1.00 40.56016
## 64 1.00 68.23588
## 65 0.67 74.47295
## 66 0.67 72.80179
## 67 0.75 31.23005
## 68 1.00 53.13132
## 69 1.00 59.36399
## 70 1.00 38.83975
## 71 1.00 28.59278
## 72 1.00 46.65884
## 73 0.75 39.10617
## 74 1.00 27.75330
## 75 0.67 49.78744
## 76 1.00 51.59219
## 77 0.75 36.18756
cereal.numeric.matrix <- as.matrix(cereal_tb1.removed)
str(cereal.numeric.matrix)
## num [1:77, 1:13] 70 120 70 50 110 110 110 130 90 90 ...
## - attr(*, "dimnames")=List of 2
## ..$ : NULL
## ..$ : chr [1:13] "calories" "protein" "fat" "sodium" ...
summary function to extract the median, 1st
quartile and 3rd quartile data from the sodium column.summary(cereal$sodium)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 0.0 130.0 180.0 159.7 210.0 320.0
sodium (max(), min(),
sd(), mean())cereal_tb1 %>% pull(sodium) %>% summary()
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 0.0 130.0 180.0 159.7 210.0 320.0
sodium of each mfr.cereal %>% group_by(mfr) %>% summarise(mean(sodium))
## # A tibble: 7 × 2
## mfr `mean(sodium)`
## <chr> <dbl>
## 1 A 0
## 2 G 200.
## 3 K 175.
## 4 N 37.5
## 5 P 146.
## 6 Q 92.5
## 7 R 198.
sodium against mfr
using boxplot().boxplot(sodium ~ mfr,data = cereal,main="sodium ~ mfr Boxplot")
calories against sodium using
plot().plot(calories ~ sodium,data = cereal,main="Something")
x <- cereal$sodium
y <- cereal$calories
plot(x,y)
plot(cereal$sodium,cereal$calories)
ggplot(data = cereal, aes(x = sodium, y = calories)) +
geom_point() +
geom_smooth(method = "lm", formula = y ~ x, se = FALSE)
kelloggs.csv. Use the write.csv
command.write.csv(kelloogs, file = "kelloggs.csv")