Class 6
The code for the alive map:
Error message often tells you what is wrong.
What’s wrong?
my_number_checker(12)
Error in my_number_checker(12) :
could not find function "my_number_checker"How about this one?
ls320::my_number_checker(12, 2)
Error in ls320::my_number_checker(12, 2) : unused argument (2)Sometimes, it gets trickier
systemfonts/libs/systemfonts.so.dSYM/Contents/Resources/DWARF/systemfonts.so: truncated gzip input: Unknown error: -1
tar: Error exit delayed from previous errors.
Error: file ‘/var/folders/ft/75t6c_c16yq_6yyw38w131nm0000gn/T//RtmpcMt7ru/downloaded_packages/systemfonts_1.3.1.tgz’ is not a macOS binary package
In addition: There were 16 warnings (use warnings() to see them)runif, generate random number from a uniform distribution.rnorm, generate random number from a normal distribution.sample, get samples from a vector.In terms of reproducibility, it is a MUST to set seed (function set.seed()) every time before using any simulation and sampling functions.
Different seed will control to generate different numbers.
set.seed() sets the random number generator state. All random functions draw from and advance this state. It is good practice to call set.seed() immediately before a random operation every time if you want that result to be reproducible.
Take a guess: is it completely safe?
Create the following 1-d structures (1):
a: a random vector of integers with 10 elements drawn from 1-20:
sample function with set.seed(10)a with a vector of names starting with “V1” and ending with “V10”.
paste0 function to create those names.paste function.Create the following 1-d structures (2):
b: randomly select 10 elements from letters using seed 10.
d: a random vector of integers with 10 elements from a normal distribution with a mean = 100 and an sd of 20:
rnorm function with set.seed(12).Q: why do we skip c?
Create the following 1-d structures (3):
l from a, b, d.
l’s element.Create the following 2-d structures (1):
m: a matrix with three integer columns named “V1”, “V2”, “V3”
V1 is 1:10V2 is a random sample between 1:100 using set.seed(50)V3 is drawn from a random uniform distribution (runif) between 0 and 50. Use the same seed as before.str and class of mCreate the following 2-d structures (2):
dat, a data.frame built from V1, V2, V3, and V4
V4 is a random selection of the letters A-E (Try LETTERS?), use the same seed as above.Vectors
x[c(1, 3, 5)]x[-c(1:2)]x[c('name1', 'name3')]x[c(F, T, T, F)]. But be careful of the length.x[]. Got a full vectorx[0]. Got a vector with length 0Be careful of index out of bound!!
Array/Matrix
m[row, col], the thing before comma is row, and the thing after comma is the col.a[row, col, higher-dim].m[1:3, ] or a[c(1, 3, 4), , c(T, F, F, T)].List
l[1], l[1:3], l[c(T, F, T)], l[c('name1', 'name3')].[[]] or $. E.g. l[[1]] or l[1][[1]], l[['id']], l$id.[[]] only can get one item from a list as a vector each time.df[row, col] works for data.frame.df[col], df[c('col_name')].[[]] and $ syntax to get the simple vector also works. E.g. df[1][[1]], df[['id']], df$id.All indexing methods to get the values out can also be used to change values by just assigning new values.
aa named V1, V2, V3 (use the names)a with the word “sasquatch”
Practice using logical for indexing:
b the values “c”, “d”, “e” if there is any (%in%)
%in% by reading its documentation.b of values “c”, “d”, “e” if there is any
whichd and the last 5 values of d into two separate vectors and multiply them.d all values > 100:
d values between 95 - 105, and replace them with 100Repeat these steps, but do it by accessing a, b, and d from l:
aa with the word “sasquatch”
b the values “c”, “d”, “e” if there is any (%in%)
%in% by reading its documentation.d values between 95 - 105, and replace them with 100m.m
m, and replace them with their values multiplied by 10Continue:
dat the values of V3, and square them. Do it using index notation in different ways, such as column name in [], and $dat into a new data.frame datss.dat rows 1:2, column 1:2 with the values -1:-4dat you just changed with the values in datssOnly works for numbers. Try these for m:
rowsSumscolSumsrowMeanscolMeans