Datacamp : Intermediate R : Flashcards
Hello > Goodbye , Trues or False?
True since H comes after G , hello is consider greater than goodbye
R determines the greater than relationship based on alphabetical order. A
R is very good at vectors?
True
OR , AND and NOT operator?
I , & and !
What is diff bwen I and II in R?
The | and || operators indicate the Logical OR.
The short form ( | ) operates element-wise on vectors and returns a vector of the same size as the input vectors. If necessary it recycles the shorter vector.
The long form ( || ) only considers the first element of each vector and returns a logical vector of length one.
Example:
> c(FALSE, FALSE) | c(TRUE, FALSE)
[1] TRUE FALSE
> c(FALSE, FALSE) || c(TRUE, FALSE)
[1] FALSE
Explanation:
In the first case, we used the ‘|’ operator, therefore it operated element-wise and returned a vector of the same size, in this case ‘2’.
In the second case, we used the ‘||’ operator, therefore it operated only on the first element of both the vectors and returned a logical vector of length one.
Thanks! not use
What is diff btw & and && in R?
That is because & works on each element and && doesnt.
Remember && is usually used in control-flow statements like if, while, etc
There’s a tricky bit about R‘s logical operators, and though it’s described in the help pages that you get when you type, for example, ?"&" I still fall from it from time to time. (I think it has to do with my rudimentary knowledge and usage of “&&” for if-statements in bash shell scripts. Below I just go through “&&” and “&”. Obviously, this also applies to “||” and “|”. Here’s a little demo to remind me and to demonstrate this to newbies: > 1==1 [1] TRUE > 1==1 & 1==2 [1] FALSE > 1==1 && 1==2 [1] FALSE So far, so good. Now look what happens if we apply this to vectors: > 1:3==1:3 [1] TRUE TRUE TRUE > 1:3==c(1,3,3) [1] TRUE FALSE TRUE > 1:3==1:3 & 1:3==c(1,3,3) [1] TRUE FALSE TRUE This is what you probably want in most cases: element-wise comparison. The “&” here compares each element in the vector “TRUE TRUE TRUE” to the corresponding element in “TRUE FALSE TRUE” and returns a “TRUE” each time they match and a “FALSE” if they don’t. Now look what happens if we use “&&”: > 1:3==1:3 && 1:3==1:3 [1] TRUE > 1:3==1:3 && 1:3==c(1,3,3) [1] TRUE For “&&”, as the R help page says, “The longer form evaluates left to right examining only the first element of each vector.” This is quite obvious in this example, but it can get confusing when you use a logical operator to index a vector. Watch this: > x x[x<5] [1] 1 2 3 4 > x[x<5 & x>2] [1] 3 4 Fine. > x[x<5 && x>2] integer(0) The reason: > x>2 & x<5 [1] FALSE FALSE TRUE TRUE FALSE > x>2 && x<5 [1] FALSE So in the first case, R "sees" > x[c(FALSE,FALSE,TRUE,TRUE,FALSE)] and in the second case, it sees: > x[FALSE] As long as the first element compares with "FALSE", you're actually lucky, because the error will be obvious. It's tricky when "&&" evaluates to "TRUE" when it looks at the first element, because some R functions will recycle input if it is too short, and this can lead to things like: > x y x==y [1] TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE > x[x==1 & y==1] [1] 1 > x[x==1 && y==1] [1] 1 2 3 4 5 6 7 8 9 10 So basically, for logical comparison, stick to "&" unless you know you need "&&". By the way, if you want to see the values that are elements of both x an y, use "%in%": > x[x%in%y] [1] 1 3 4 5 6 7 8 9 10 > y[y%in%x] [1] 1 3 4 5 6 7 8 9 10 But don't do: > y[x%in%y] [1] 1 4 5 6 7 8 9 10 11 This indexes y using the logical vector returned by "x%in%y", which is of course: > x%in%y [1] TRUE FALSE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
& and &&
&& = examine the first element of each vector , & examine element wise.
so also with | and ||. another diff is in control structures
That is because & works on each element and && doesnt.
Remember && is usually used in control-flow statements like if, while, etc
if else stru>
if ()
else
else
ele
nested if
if () {
}else if {
}else if
How does break in while loop behaves?
When R encounters it, the while loop is abandoned completely.
modular operator in R?
%%
for loop structure
for (var in seq ){
}
e.g for (city in cities) where cities is vector
cities = c(“a” , “b”)
for loop work on vectors or list. subsetting vectors or list is same , for loop does it automatically
for loop how to break it?
using break keyword
How to continue for loop if a condition is met?
using next , it will stop execution of current state ad continue with next state in the for loop
what are 2 versions of for loop?
for (citie in cities)..consise , easy to read , no acces to looping index
for(i in 1:legnth(cities) ),..harder to read and write , More vasatile
examples of 2 kind of for loops
primes
example looping over a vector with fo
# The linkedin vector has already been defined for you linkedin
Example of looping over list
primes_list
nexted for loop format?
for (var1 in seq1) { for (var2 in seq2) { expr } }
using paste()
print( paste(“On row” , i , “and colum “ , j, “the board contains” , ttt[i,j] ))
How to split a string variable ?
This variable has been split up into a vector that contains separate letters and has been stored in a vector chars with the strsplit() function.
rquote
way of printing combine string and digit in R?
print(paste(“The year is”, year))