norm_vec
with length \(n=1000\) from the normal distribution with
mean 1 and variance 5.Write a for()
loop with if()
statement
to count the number of entries in norm_vec
that are
strictly larger than 0.
Re-do the above task without using any for()
loop
(i.e., apply the build-in function in R with logical vector
operation).
Is the execution time of using for()
loop longer
than the vectorization? (Return a logical
TRUE/FALSE
).
set.seed(123) ## Don't change this line. It makes the result reproducible.
# Your code here
while
and
repeat
.)set.seed(123) ## Don't change this line. It makes the result reproducible.
# Your code here (use `while`)
set.seed(123) ## Don't change this line. It makes the result reproducible.
# Your code here (use `repeat`)
a
that contains all the even numbers
ranging from -4 to 10. Compute the cosine value of each entry using the
sapply()
function.# Your code here
lm()
function is used to run a linear regression.linearMod = lm(dist ~ speed, data = cars)
What are the names of the elements in the list
linearMod
?
Compute the mean and variance of each column of the
model
element in the list linearMod
using
apply()
function.
Compute the length of each element in the list
linearMod
. How many of them have length 2?
# Your code starts from here
We will examine data from the 2016 Summer Olympics in Rio de Janeiro,
originally taken from https://github.com/flother/rio2016.
Below, we read in the data and store it as rio
. All the
following questions will be answered based on rio
.
rio =
read.csv(url("https://github.com/zhangyk8/zhangyk8.github.io/raw/master/_teaching/file_stat302/Data/rio.csv"),
header = TRUE)
rio
? What are its dimensions and
columns names of rio? Is there any missing data (i.e.,
NA
)?# Your code starts from here
duplicated()
.)# Your code starts from here
f
, you can use
table(f)
to see how many elements in f
are in
each level of the factor.)# Your code starts from here
TRUE/FALSE
.)# Your code starts from here
total
in the data frame
rio
which adds the number of gold, silver, and bronze
medals for each athlete. Which athlete had the most number of
medals?# Your code starts from here
tapply()
, calculate the total medal count for
each country. Save the result as tot_nat
. Which country had
the most number of medals, and how many was this? How many countries had
zero medals?# Your code starts from here