HC <- scan("HonorCode.txt", what = "")
head(HC, 15)
##########################
# square input: write my own function
square_it <- function(x){
out <- x*x
return(out)
}
##########################
# write a func to split the words by words.
findwords <- function(text_vec){
words <- split(1:length(text_vec), text_vec)
return(words)
}
# output is list bc: we cannot make sure each line are same
# for bose previous and below function
##########################
# write a func to alphabetize the word list
alphabetized_list <- function(wordlist) {
nms <- names(wordlist) # The names are the words
sorted <- sort(nms) # The words, but now in ABC order
return(wordlist[sorted]) # Returns the sorted version +}
A control statement determines whether other statements will or will not be executed.
For
for (i in x){
...
}
# Increments a counter i along a vector x.
# Loops through the body of the statement
# (between { and }) until thecounter runs through the vector.
Example of for loop
# each dataset n = 50
# simulate 1k data uniform(0,1)
# compute the medium for each dataset and store the mediums in a vector of leangth 1000
my_med <- NULL
r <- 1000
n <- 50
mym <- rep(NA, r)
for(i in 1:r){
mym[i] <- median(runif(n))
}
hist(mym)