# problem 1
age <- c(18, 19, 23,19, 24, 20, 18, 21, 22, 23,18)
# space for problem 1b
mean(age)
## [1] 20.45455
median(age)
## [1] 20
min(age)
## [1] 18
max(age)
## [1] 24
range(age)
## [1] 18 24
quantile(age)
## 0% 25% 50% 75% 100%
## 18.0 18.5 20.0 22.5 24.0
sd(age)
## [1] 2.252272
var(age)
## [1] 5.072727
summary(age)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 18.00 18.50 20.00 20.45 22.50 24.00
length(age)
## [1] 11
sum(age)
## [1] 225
IQR(age)
## [1] 4
table(age)
## age
## 18 19 20 21 22 23 24
## 3 2 1 1 1 2 1
# space for problem 1c
?IQR
#The question mark in front of a function pulls up a help page that explains the use of the function. For instance, when the IQR is used, R computes the interquatile range of the values in the indicated dataset.
# space for problem 1d
?sd
#The denomintor of this function is n-1, thus it calculates sample standard deviation.
#----------------------------
# problem 2
# problem 2a
score <- c(75, 77, 94, 75, 79, 80, 66, 82, 86, 80, 78)
# problem 2b
id <- c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11)
# problem 2c
df <- data.frame(id = id, age = age, score = score)
print(df)
## id age score
## 1 1 18 75
## 2 2 19 77
## 3 3 23 94
## 4 4 19 75
## 5 5 24 79
## 6 6 20 80
## 7 7 18 66
## 8 8 21 82
## 9 9 22 86
## 10 10 23 80
## 11 11 18 78
# problem 2d
df[,1] #This extracts all of the first column, or all the id numbers
## [1] 1 2 3 4 5 6 7 8 9 10 11
df[,2] #This extracts all of the second column, which are the ages
## [1] 18 19 23 19 24 20 18 21 22 23 18
df[1,1] #This gives row 1, column 1
## [1] 1
df[2,2] #This gives row 2, column 2
## [1] 19
df[1,] #This extracts all of the first row
## id age score
## 1 1 18 75
df[2,] #This extracts all of the second row
## id age score
## 2 2 19 77
df$id #This gives the full id column
## [1] 1 2 3 4 5 6 7 8 9 10 11
df["id"] #This gives the full id column, but as dataframe format
## id
## 1 1
## 2 2
## 3 3
## 4 4
## 5 5
## 6 6
## 7 7
## 8 8
## 9 9
## 10 10
## 11 11
df$id[3] #This extracts the third value of the id column
## [1] 3
df$age #This gives the full age column
## [1] 18 19 23 19 24 20 18 21 22 23 18
df$score #This gives the full score column
## [1] 75 77 94 75 79 80 66 82 86 80 78
df["score"] #This gives the full score column in dataframe format
## score
## 1 75
## 2 77
## 3 94
## 4 75
## 5 79
## 6 80
## 7 66
## 8 82
## 9 86
## 10 80
## 11 78
#------------------------
# Problem 3
# Problem 3a
getwd()
## [1] "/cloud/project/Biostatistics Lab"
setwd("/cloud/project") #Hi I am running R in Posit cloud, so the setwd goes to my Posit cloud projects instead of my computer files.
list.files()
## [1] "Biostatistics 100 – Lab 1.nb.html" "Biostatistics 100 – Lab 1.Rmd"
## [3] "Biostatistics Lab" "Lab1.R"
## [5] "project.Rproj" "testData.txt"
#Problem 3b
df2 <- read.table('testData.txt', sep = ',', header=TRUE)
colnames(df2) <- c("id", "age", "score")
df2 <- as.data.frame(df2)
df2
## id age score
## 1 2 19 77
## 2 3 23 94
## 3 4 19 75
## 4 5 24 79
## 5 6 20 80
## 6 7 18 66
## 7 8 21 82
## 8 9 22 86
## 9 10 23 80
## 10 11 18 78
#----------------------
# Problem 4
#Problem 4a
age2 <- age^2
age2
## [1] 324 361 529 361 576 400 324 441 484 529 324
df$age2 <- age^2
age + 1 #This command added 1 to all the age values
## [1] 19 20 24 20 25 21 19 22 23 24 19
age + age # This command doubled all of the age values
## [1] 36 38 46 38 48 40 36 42 44 46 36
# ------------------------
# Problem 5
age[1] <- 19
age
## [1] 19 19 23 19 24 20 18 21 22 23 18
df[1,2] <- 19
df
## id age score age2
## 1 1 19 75 324
## 2 2 19 77 361
## 3 3 23 94 529
## 4 4 19 75 361
## 5 5 24 79 576
## 6 6 20 80 400
## 7 7 18 66 324
## 8 8 21 82 441
## 9 9 22 86 484
## 10 10 23 80 529
## 11 11 18 78 324