Lab Assignment 1 (Lab projects?)

R and RStudio continue

Follow the below instructions to make yourself comfortable in using R/RStudio for statistical/scientific computing.

Working directory

Working directory is the default folder in which files are saved and read.

  • In the Files tab, find/create a folder for your STA100 work. Click More then Set As Working Directory.

  • Open the R script you worked on last week. Run the codes.

  • Working directory is not persistent after you you close the R/RStudio. Remember to set working directory each time you open a R/RStudio.

Simulations/Sampling

  • Let’s draw a sample of random numbers which follows normal distribution with mean 2.5 and standard deviation 1.2
n <- 1000
x <- rnorm(n, mean = 2.5, sd = 1.2)
hist(x)

mean(x)
## [1] 2.508808
sd(x)
## [1] 1.196989
  • Let’s draw random samples from BTS

bts <- c("Jin", "Suga", "J-Hope", "RM", "Jimin", "V", "Jungkook")

What is the size of bts?

length(bts)
## [1] 7

Sampling!

subgroup_a <- sample(bts, 4, replace = FALSE)
subgroup_b <- sample(bts, 4, replace = FALSE)
subgroup_a
## [1] "Suga"   "Jimin"  "J-Hope" "V"
subgroup_b
## [1] "Jungkook" "Suga"     "Jin"      "J-Hope"

What are the union and intersection of the subgroups?

union(subgroup_a, subgroup_b)
## [1] "Suga"     "Jimin"    "J-Hope"   "V"        "Jungkook" "Jin"
intersect(subgroup_a, subgroup_b)
## [1] "Suga"   "J-Hope"
Set Elements Size
BTS (Population) Jin, Suga, J-Hope, RM, Jimin, V, Jungkook 7
Subgroup A (Sample) Suga, Jimin, J-Hope, V 4
Subgroup B (Sample) Jungkook, Suga, Jin, J-Hope 4
A or B (Union) Suga, Jimin, J-Hope, V, Jungkook, Jin 6
A and B (Intersect) Suga, J-Hope 2

Help/Documentation

Read the R Documentation of the functions you learn today. Enter the following codes, one by one, and read the corresponding documentation/help in the Help tab.

?rnorm
?mean
?sd
?hist
?length
?sample
?union
?intersect

How Po knows all those built-in functions in R?

  • Way 1: Google it

  • Way 2: Read R Help/Documentation

Give a man a fish and you feed him for a day; Teach a man to fish and you feed him for a lifetime.

Po Codes in labs Google R Documentation
Fisherman Fish Fish Rod Sea

More R tutorial