Using R on our computer
(4 + 6)^3 - 2*500/4
## [1] 750
quantity <- c(34, 56, 22)
price <- c(19.95, 14.95, 10.99)
subtotal <- quantity*price
cbind(quantity, price, subtotal)
## quantity price subtotal
## [1,] 34 19.95 678.30
## [2,] 56 14.95 837.20
## [3,] 22 10.99 241.78
Useful R concepts
4*4
## [1] 16
x <- 4 * 4
x
## [1] 16
(x <- 4 * 4)
## [1] 16
x <- 4*4; x
## [1] 16
xx <- "hello, what's your name"; xx
## [1] "hello, what's your name"
aa <- bb <- 5
aa; bb
## [1] 5
## [1] 5
cc <- aa * bb; cc
## [1] 25
cc <- aa * bb; cc
## [1] 25
Useful R functions
ls()[1:4]
## [1] "aa" "bb" "cc" "price"
search()
## [1] ".GlobalEnv" "package:stats" "package:graphics"
## [4] "package:grDevices" "package:utils" "package:datasets"
## [7] "package:methods" "Autoloads" "package:base"
args(sample)
## function (x, size, replace = FALSE, prob = NULL)
## NULL
dates <- 1:30
sample(dates, 16)
## [1] 21 27 25 9 6 7 12 14 28 8 22 1 11 17 20 15
sample(size = 16, x = dates)
## [1] 4 15 29 8 20 24 9 11 12 16 2 1 30 17 14 25
sample(s = 16, x = dates, r = TRUE)
## [1] 8 1 11 21 30 24 3 6 26 19 12 10 28 13 12 14
sample(s = sample(1:36, 1), x = sample(1:10, 5), r=T)
## [1] 10 2 7 7 9 9 10 8 8 9 2 2 8 8 9 2 8 2 8 8 9 2 2
## [24] 10 2 2 9 7
Working with R objects
x <- c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12); x
## [1] 1 2 3 4 5 6 7 8 9 10 11 12
y <- matrix(x, nrow = 2); y
## [,1] [,2] [,3] [,4] [,5] [,6]
## [1,] 1 3 5 7 9 11
## [2,] 2 4 6 8 10 12
z <- array(x, dim = c(1, 6, 2)); z
## , , 1
##
## [,1] [,2] [,3] [,4] [,5] [,6]
## [1,] 1 2 3 4 5 6
##
## , , 2
##
## [,1] [,2] [,3] [,4] [,5] [,6]
## [1,] 7 8 9 10 11 12
mylist <- list(x, y, z); mylist
## [[1]]
## [1] 1 2 3 4 5 6 7 8 9 10 11 12
##
## [[2]]
## [,1] [,2] [,3] [,4] [,5] [,6]
## [1,] 1 3 5 7 9 11
## [2,] 2 4 6 8 10 12
##
## [[3]]
## , , 1
##
## [,1] [,2] [,3] [,4] [,5] [,6]
## [1,] 1 2 3 4 5 6
##
## , , 2
##
## [,1] [,2] [,3] [,4] [,5] [,6]
## [1,] 7 8 9 10 11 12
smoker <- c("Y", "Y", "N", "N", "Y", "Y", "Y", "N", "Y")
cancer <- c("Y", "N", "N", "Y", "Y", "Y", "N", "N", "Y")
mydf <- data.frame(smoker, cancer);
mydf[1:4,]
## smoker cancer
## 1 Y Y
## 2 Y N
## 3 N N
## 4 N Y
Graphical Models
xtabs(~smoker + cancer, data = mydf)
## cancer
## smoker N Y
## N 2 1
## Y 2 4
Precision and number types
typeof(3)
## [1] "double"
typeof(3L)
## [1] "integer"
typeof(as.integer(3))
## [1] "integer"
typeof(4L/2L)
## [1] "double"
typeof(as.integer(4L/2L))
## [1] "integer"
getwd()
## [1] "/Users/dr.auwal/Desktop/Personals/UC Berkeley/Classes/PB HLTH 251D Applied Epidemiology Using R/Homework/Homework 1"
.RData
getwd()
## [1] "/Users/dr.auwal/Desktop/Personals/UC Berkeley/Classes/PB HLTH 251D Applied Epidemiology Using R/Homework/Homework 1"
Homework 1
search()
## [1] ".GlobalEnv" "package:stats" "package:graphics"
## [4] "package:grDevices" "package:utils" "package:datasets"
## [7] "package:methods" "Autoloads" "package:base"
searchpaths()
## [1] ".GlobalEnv"
## [2] "/Library/Frameworks/R.framework/Versions/3.6/Resources/library/stats"
## [3] "/Library/Frameworks/R.framework/Versions/3.6/Resources/library/graphics"
## [4] "/Library/Frameworks/R.framework/Versions/3.6/Resources/library/grDevices"
## [5] "/Library/Frameworks/R.framework/Versions/3.6/Resources/library/utils"
## [6] "/Library/Frameworks/R.framework/Versions/3.6/Resources/library/datasets"
## [7] "/Library/Frameworks/R.framework/Versions/3.6/Resources/library/methods"
## [8] "Autoloads"
## [9] "/Library/Frameworks/R.framework/Resources/library/base"
ls()
## [1] "aa" "bb" "cancer" "cc" "dates" "mydf"
## [7] "mylist" "price" "quantity" "smoker" "subtotal" "x"
## [13] "xx" "y" "z"
10%/%3
## [1] 3
10%%3
## [1] 1
curve(log(x), 0, 6)
abline(v = c(1, exp(1)), h = c(0, 1), lty = 2)
curve(x/(1-x), 0, 1)
curve(log(x/(1-x)), 0, 1)
n <- 365
per.act.risk <- c(67, 50,30,10,6.5,5,1,0.5)/10000
risks <- 1-(1-per.act.risk)^n
show(risks)
## [1] 0.91402762 0.83951869 0.66601052 0.30593011 0.21126678 0.16685338
## [7] 0.03584367 0.01808493
curve(log(x), 0, 6)
abline(v = c(1, exp(1)), h = c(0, 1), lty = 2)
sink() #closes connection
n <- 365
per.act.risk <- c(0.5, 1, 5, 6.5, 10, 30, 50, 67)/10000
risks <- 1-(1-per.act.risk)^n
show(risks)
## [1] 0.01808493 0.03584367 0.16685338 0.21126678 0.30593011 0.66601052
## [7] 0.83951869 0.91402762