1/11
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai | Chat |
|---|
No analytics yet
Send a link to your students to track their progress
What does Is() do?
Shows what has been defined in the environment
What is the output of the following code —> sum(c(TRUE,TRUE,FALSE,TRUE))
3 because a logic value is 1 for TRUE and 0 for FALSE
What is the output of the code c(1,10,100) * 1.5?
1.5 15.0 150.0
Given x ← c(a = 10, b = 20, c = 30)
x1 ← x[c(2,3)]
Are x[2] and x1[2] the same? And are x[‘b’] and x1[‘b’] the same? Briefly explain why or why not
x[2] and x1[2] are not the same because x[2] refers to b 20 and x1[2] refers to c 30, since the second position of x1 is the third position of x
x[‘b’] and x1[‘b’] are the same because they refer to names over position and since both x and x1 have ‘b’, they both output b = 20
What is the difference between identical() and all.equal()
Identical() asks if these two objects are the exact same, down to how they look
If you had 0.5-0.3 and 0.3-0.1, it would produce FALSE because they are not the same despite both equaling 0.2
all.equal() asks if these values are equal, so in the previous example, it would be TRUE because both statements equate to 0.2
TRUE or FALSE
Run the code xx <- c(a=1, b=2)
Object xx does not have an attribute "names", because it does not use function attr() to add this attribute
FALSE because the names a and b auto make a names attribute for xx even if you didn’t use attr()
What does “rep” do?
rep Replicates the elements in a vector, it works like this
rep( value/s, x amount of times)
So rep(1,5) = 1 1 1 1 1 and
rep(c(1,2,3),4) = 1 2 3 1 2 3 1 2 3 1 2 3
What happens if you rep(c(1,2,3), c(2,1,4)
1 1 2 3 3 3 3
The number in each vector comes out as the amount on the other vector, so since it’s 2 in the first vector, it’s going to be 2 1s
What does “seq” do
seq makes a sequence of equally spaced numbers on a linear scale
so seq(1,15,2) works like (start, end, slope). This comes out as
1 3 5 7 9 11 13 15
How to use if and else together
Start with the if
if (x > 0.5) {
Then use a curly bracket to group the expression
cat(“head\n)
y ← 1
Then use else
} Else {
cat(“tail\n”)
y ← 0
}
Logical Conditions
FALSE || TRUE = TRUE
NA || TRUE = TRUE
FALSE | TRUE = TRUE
FALSE && TRUE = FALSE
NA && TRUE = NA
T || F || F = TRUE