options(
repos = c(CRAN = "https://cloud.r-project.org"),
timeout = 300,
download.file.method = "wininet"
)
install.packages(
"rlang",
type = "binary"
)
## Installing package into 'C:/Users/derek/AppData/Local/R/win-library/4.5'
## (as 'lib' is unspecified)
## Warning in download.file(url, destfile, method, mode = "wb", ...): the
## 'wininet' method is deprecated for http:// and https:// URLs
## package 'rlang' successfully unpacked and MD5 sums checked
## Warning: cannot remove prior installation of package 'rlang'
## Warning in file.copy(savedcopy, lib, recursive = TRUE): problem copying
## C:\Users\derek\AppData\Local\R\win-library\4.5\00LOCK\rlang\libs\x64\rlang.dll
## to C:\Users\derek\AppData\Local\R\win-library\4.5\rlang\libs\x64\rlang.dll:
## Permission denied
## Warning: restored 'rlang'
##
## The downloaded binary packages are in
## C:\Users\derek\AppData\Local\Temp\RtmpSigme9\downloaded_packages
install.packages(
"rsconnect",
type = "binary"
)
## Installing package into 'C:/Users/derek/AppData/Local/R/win-library/4.5'
## (as 'lib' is unspecified)
## Warning in download.file(url, destfile, method, mode = "wb", ...): the
## 'wininet' method is deprecated for http:// and https:// URLs
## package 'rsconnect' successfully unpacked and MD5 sums checked
##
## The downloaded binary packages are in
## C:\Users\derek\AppData\Local\Temp\RtmpSigme9\downloaded_packages
library(tidyverse)
library(dplyr)
options(
repos = c(CRAN = "https://cloud.r-project.org"),
timeout = 300,
download.file.method = "wininet"
)
the beginning of a new new life
url <- paste0(
"https://raw.githubusercontent.com/",
"PacktPublishing/",
"The-Statistics-and-Machine-Learning-with-R-Workshop/",
"main/Chapter_2/data/"
)
file1 = 'question_tags.csv'
file2 = 'questions.csv'
file3 = 'tags.csv'
question_tags <- read.csv(paste0(url,file1))
questions = read.csv(paste0(url,file2))
tags = read.csv(paste0(url,file3))
head(question_tags)
## question_id tag_id
## 1 22557677 18
## 2 22557677 139
## 3 22557677 16088
## 4 22557677 1672
## 5 22558084 6419
## 6 22558084 92764
head(tags)
## id tag_name
## 1 124399 laravel-dusk
## 2 124402 spring-cloud-vault-config
## 3 124404 spring-vault
## 4 124405 apache-bahir
## 5 124407 astc
## 6 124408 simulacrum
head(questions)
## id creation_date score
## 1 22557677 2014-03-21 1
## 2 22557707 2014-03-21 2
## 3 22558084 2014-03-21 2
## 4 22558395 2014-03-21 2
## 5 22558613 2014-03-21 0
## 6 22558677 2014-03-21 2
data("iris")
class("iris")
## [1] "character"
View(iris)
iris = data.frame(iris)
iris_tbl = as_tibble(iris)
class(iris_tbl)
## [1] "tbl_df" "tbl" "data.frame"
View(iris_tbl)
Data transformations with dplyr
filter() slicing the data set pip operator %>%
arrange() mutate() select() top_n()
# Exercise 2.02 - filtering using the pipe operator
# keep only the setosa species in the iris dataset using the pipe operator and the filter() function
unique(iris_tbl$Species)
## [1] setosa versicolor virginica
## Levels: setosa versicolor virginica
# Levels: setosa versicolor virginica
# keep only the setosa species in iris_tbl using filter() and save the results
iris_tbl_subset = iris_tbl %>%
filter(Species == 'setosa')
head(iris_tbl_subset)
## # A tibble: 6 × 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
unique(as.character(iris_tbl_subset$Species))
## [1] "setosa"
# can add a series of filters
iris_tbl_subset = iris_tbl %>%
filter(Species == "setosa",
Sepal.Length <=5)
max(iris_tbl_subset$Sepal.Length)
## [1] 5
Sorting the data set using the arrange() function
# Exercise 2.03 sorting using the arrange() function
# default is ascending
iris_tbl_sorted = iris_tbl %>%
arrange(Sepal.Length)
iris_tbl_sorted
## # A tibble: 150 × 5
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## <dbl> <dbl> <dbl> <dbl> <fct>
## 1 4.3 3 1.1 0.1 setosa
## 2 4.4 2.9 1.4 0.2 setosa
## 3 4.4 3 1.3 0.2 setosa
## 4 4.4 3.2 1.3 0.2 setosa
## 5 4.5 2.3 1.3 0.3 setosa
## 6 4.6 3.1 1.5 0.2 setosa
## 7 4.6 3.4 1.4 0.3 setosa
## 8 4.6 3.6 1 0.2 setosa
## 9 4.6 3.2 1.4 0.2 setosa
## 10 4.7 3.2 1.3 0.2 setosa
## # ℹ 140 more rows
# descending order
iris_tbl_sorted = iris_tbl %>%
arrange(desc(Sepal.Length))
iris_tbl_sorted
## # A tibble: 150 × 5
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## <dbl> <dbl> <dbl> <dbl> <fct>
## 1 7.9 3.8 6.4 2 virginica
## 2 7.7 3.8 6.7 2.2 virginica
## 3 7.7 2.6 6.9 2.3 virginica
## 4 7.7 2.8 6.7 2 virginica
## 5 7.7 3 6.1 2.3 virginica
## 6 7.6 3 6.6 2.1 virginica
## 7 7.4 2.8 6.1 1.9 virginica
## 8 7.3 2.9 6.3 1.8 virginica
## 9 7.2 3.6 6.1 2.5 virginica
## 10 7.2 3.2 6 1.8 virginica
## # ℹ 140 more rows
######################################
### sort both Sepal.Length and Sepal.Width in descending order after keeping Species set tuo "setosa"
iris_tbl_subset_sorted = iris_tbl %>%
filter(Species == 'setosa',
Sepal.Length <= 5) %>%
arrange(desc(Sepal.Length), desc(Sepal.Width))
iris_tbl_subset_sorted
## # A tibble: 28 × 5
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## <dbl> <dbl> <dbl> <dbl> <fct>
## 1 5 3.6 1.4 0.2 setosa
## 2 5 3.5 1.3 0.3 setosa
## 3 5 3.5 1.6 0.6 setosa
## 4 5 3.4 1.5 0.2 setosa
## 5 5 3.4 1.6 0.4 setosa
## 6 5 3.3 1.4 0.2 setosa
## 7 5 3.2 1.2 0.2 setosa
## 8 5 3 1.6 0.2 setosa
## 9 4.9 3.6 1.4 0.1 setosa
## 10 4.9 3.1 1.5 0.1 setosa
## # ℹ 18 more rows
Adding or changing a column using the mutate() function