This is an R Markdown Notebook. When you execute code within the notebook, the results appear beneath the code.
Try executing this chunk by clicking the Run button within the chunk or by placing your cursor inside it and pressing Ctrl+Shift+Enter.
library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.2 ──
## ✔ ggplot2 3.3.6 ✔ purrr 0.3.4
## ✔ tibble 3.1.8 ✔ dplyr 1.0.9
## ✔ tidyr 1.2.0 ✔ stringr 1.4.0
## ✔ readr 2.1.2 ✔ forcats 0.5.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
library(nycflights13)
Problem 1:
table(filter(flights, month >= 7, month <= 9)[,"month"])
## month
## 7 8 9
## 29425 29327 27574
table(filter(flights, month %in% 7:9)[,"month"])
## month
## 7 8 9
## 29425 29327 27574
table(flights[flights$month %in% 7:9, "month"])
## month
## 7 8 9
## 29425 29327 27574
(dts <- c("2019/02/24 24:00:00", "2019/02/25 00:00:00",
"2019/02/25 00:00:00.01", "2019/02/24 23:59:59.999999"))
## [1] "2019/02/24 24:00:00" "2019/02/25 00:00:00"
## [3] "2019/02/25 00:00:00.01" "2019/02/24 23:59:59.999999"
(dts.posixct <- as.POSIXct(dts))
## [1] "2019-02-25 00:00:00 PST" "2019-02-25 00:00:00 PST"
## [3] "2019-02-25 00:00:00 PST" "2019-02-24 23:59:59 PST"
dts.posixct[1] == dts.posixct[2]
## [1] TRUE
dts.posixct[2] - dts.posixct[1]
## Time difference of 0 secs
dts.posixct[3] - dts.posixct[1]
## Time difference of 0.00999999 secs
dts.posixct[3] - dts.posixct[2]
## Time difference of 0.00999999 secs
dts.posixct[2] - dts.posixct[4]
## Time difference of 9.536743e-07 secs
my.mean <- function(x){
xsum <- 0
n <- length(x)
for(i in 1:n){
xsum <- xsum + x[i]
}
return(xsum/n)
}
my.mean.1 <- function(x){
xsum <- 0
n <- length(x)
nn <- 0
for(i in 1:n){
xsum <- xsum + x[i]
nn <- nn + 1
}
return(xsum/nn)
}
my.mean.2 <- function(x){
xsum <- 0
n <- length(x)
for(y in x){
xsum <- xsum + y
}
return(xsum/n)
}
my.mean.3 <- function(x){
xsum <- 0
n <- 0
for(y in x){
xsum <- xsum + y
n <- n + 1
}
return(xsum/n)
}
my.mean.4 <- function(x){
sum(x)/length(x)
}
my.mean.5 <- function(x){
n <- length(x)
t(rep(1,n)) %*% x / n
}
library(microbenchmark)
testdat <- rnorm(100000)
microbenchmark(my.mean(testdat),
my.mean.1(testdat),
my.mean.2(testdat),
my.mean.3(testdat),
my.mean.4(testdat),
my.mean.5(testdat),
mean(testdat),
times=1000)
my.mean(testdat)
## [1] -0.0008253353
my.mean.1(testdat)
## [1] -0.0008253353
my.mean.2(testdat)
## [1] -0.0008253353
my.mean.3(testdat)
## [1] -0.0008253353
my.mean.4(testdat)
## [1] -0.0008253353
my.mean.5(testdat)
## [,1]
## [1,] -0.0008253353
mean(testdat)
## [1] -0.0008253353
my.mean(c("a","b","c","d"))
## Error in xsum + x[i]: non-numeric argument to binary operator
my.mean.2(c("a","b","c","d"))
## Error in xsum + y: non-numeric argument to binary operator
mean(c("a","b","c","d"))
## Warning in mean.default(c("a", "b", "c", "d")): argument is not numeric or
## logical: returning NA
## [1] NA
Add a new chunk by clicking the Insert Chunk button on the toolbar or by pressing Ctrl+Alt+I.
When you save the notebook, an HTML file containing the code and output will be saved alongside it (click the Preview button or press Ctrl+Shift+K to preview the HTML file).
The preview shows you a rendered HTML copy of the contents of the editor. Consequently, unlike Knit, Preview does not run any R code chunks. Instead, the output of the chunk when it was last run in the editor is displayed.