Datacamp : Intermediate R : Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

Hello > Goodbye , Trues or False?

A

True since H comes after G , hello is consider greater than goodbye

R determines the greater than relationship based on alphabetical order. A

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

R is very good at vectors?

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

OR , AND and NOT operator?

A

I , & and !

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is diff bwen I and II in R?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is diff btw & and && in R?

A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

& and &&

A

&& = 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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

if else stru>

A

if ()
else
else
ele

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

nested if

A

if () {
}else if {

}else if

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How does break in while loop behaves?

A

When R encounters it, the while loop is abandoned completely.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

modular operator in R?

A

%%

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

for loop structure

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

for loop how to break it?

A

using break keyword

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How to continue for loop if a condition is met?

A

using next , it will stop execution of current state ad continue with next state in the for loop

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

what are 2 versions of for loop?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

examples of 2 kind of for loops

A

primes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

example looping over a vector with fo

A
# The linkedin vector has already been defined for you
linkedin
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Example of looping over list

A

primes_list

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

nexted for loop format?

A
for (var1 in seq1) {
  for (var2 in seq2) {
    expr
  }
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

using paste()

A

print( paste(“On row” , i , “and colum “ , j, “the board contains” , ttt[i,j] ))

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

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.

A

rquote

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

way of printing combine string and digit in R?

A

print(paste(“The year is”, year))

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

we can think of function as a kind of ?

A

Blackbox that takes input and brings output

23
Q

find help on function. two ways ?

A

?min or help(min)

24
Q

In function , we have 2 ways for argument marching?

A

by position and by name?

25
Q

what does na.rm means in functions?

A
it is a logical . should missing value be removed?. Some functions, if you do not remove the missing values , they return NA.So always remove misisng values if you want reesult.by default is set to false. so set it to true
misali mean(x , TRUE).
26
Q

function format

A

x = function(args){ return()} using return(0) use to halt function execution

27
Q

function scoping.can u access variable declared inside function from outside?

A

no

28
Q

Does R passes an argument by value or ref?

A

R passes arguments by value

The title gives it away already: R passes arguments by value. What does this mean? Simply put, it means that an R function cannot change the variable that you input to that function.

29
Q

example passing by value

A
triple = function(x) {
  x = 3*x
  x
}
a = 5
triple(a)
a
Inside the triple() function, the argument x gets overwritten with its value times three. Afterwards this new x is returned. If you call this function with a variable a set equal to 5, you obtain 15. But did the value of a change? If R were to pass a to triple() by reference, the override of the x inside the function would ripple through to the variable a, outside the function. However, R passes by value, so the R objects you pass to a function can never change unless you do an explicit assignment. a remains equal to 5, even after calling triple(a).
30
Q

CRAN

A

comprehensive R Achive Network

31
Q

Diff btwn Require and library

A

However, according to the documentation for both functions (accessed by putting a ? before the function name and hitting enter), require is used inside functions, as it outputs a warning and continues if the package is not found, whereas library will throw an error.

Another benefit of require() is that it returns a logical value by default. TRUE if the packages is loaded, FALSE if it isn’t.

When used inside the script, you can avoid a dialog screen by specifying the repos parameter of install.packages(), such as
install.packages(package, repos=”http://cran.us.r-project.org”)
You can wrap require() and library() in suppressPackageStartupMessages() to, well, suppress package startup messages, and also use the parameters require(…, quietly=T, warn.conflicts=F) if needed to keep the installs quiet.

32
Q

What is the use of search()

A

it is use for checking the package loaded currnetly in an enviroment. , to look at the currently attached packages and

33
Q

when loading with library() can I use double quote or not?

A

if u use it is ok , if you do not use , it is ok

34
Q

how to create an empty vector and populate it with for loop? what is alternative with lapply?

A

cities = c( ´”abua”, “katisna” , “kaduna”)
nchar = c()
for(i in 1:length(cities)){ num_chars[i] = nchar(cities[i])

lapply(cities , nchar)

35
Q

lapply always return….iresspetive of data struture. how do u convert it to ur needs misali vector?

A

list

we can wrapt it using unlist.

unlist(lapply(cities , nchar))..This will produce vector not list

notice , the function does not include ()

36
Q

How lapply works? lapply(X, FUN, …)

A

lapply(X, FUN, …)

To put it generally, lapply takes a vector or list X(only list and vector), and applies the function FUN to each of its members. If FUN requires additional arguments, you pass them after you’ve specified X and FUN (…). The output of lapply() is a list, the same length as X, where each element is the result of applying FUN on the corresponding element of X. The FUN can return R object of diff classes. Hence ,it store result in list , cox it can store hetrogenous contents.

37
Q

Defining functions to use them only once is kind of overkill, isn’t it? That’s why you can use so-called anonymous functions in R.What are annoymous function in R?

A

Previously, you learned that functions in R are objects in their own right. This means that they aren’t automatically bound to a name. When you create a function, you can use the assignment operator to give the function a name. It’s perfectly possible, however, to not give the function a name. This is called an anonymous function:

Named function
triple = function(x) { 3 * x }

Anonymous function with same implementation
function(x) { 3 * x }

Use anonymous function inside lapply()
lapply(list(1,2,3), function(x) { 3 * x })

38
Q

lapply() vs sapply()?

A

lapply() and sapply()? The former returns a list, while the latter returns a vector that is a simplified version of this list

39
Q

What is sapply format?

A

sapply(X, FUN, …)

Here, FUN can be one of R’s built-in functions, but it can also be a function you wrote. This self-written function can be defined before hand, or can be inserted directly as an anonymous function.

40
Q

what is the meaning of l in lapply?

A

it means it returm list

41
Q

Can you subset a vector with conditional?

A

Yes example (x[x lessthan 0]) returns a vector that only contains the values that are strictly below zero

42
Q

what sapply stands for?

A

simplify apply , it try to simplify list to array

43
Q

vapply

A

similar to saply , uses lapply under the hood.esplicitly specify output format

44
Q

vapply and sapply , which is safer?

A

vapply function in R is similar to sapply, but has a pre-specified type of return value, so it can be safer (and sometimes faster) to use.

As highlighted before, vapply() can be considered a more robust version of sapply(), because you explicitly restrict the output of the function you want to apply. Converting your sapply() expressions in your own R scripts to vapply() expressions is therefore a good practice (and also a breeze!).

45
Q

vapply structure

A

vapply(X, FUN, FUN.VALUE, …, USE.NAMES = TRUE)

Over the elements insideX, the functionFUNis applied. TheFUN.VALUEargument expects a template for the return argument of this functionFUN.USE.NAMESisTRUEby default; in this casevapply()tries to generate a named array, if possible.

46
Q

is. * and as.*

A

use to check and convert to datatype.

47
Q

unlist()

A

change list to vector

48
Q

rev function in r?

A

reverse the vector or list

49
Q

grepl & grep

A

grepl & grep

In their most basic form, regular expressions can be used to see whether a pattern exists inside a character string or a vector of character strings. For this purpose, you can use:

grepl(), which returns TRUE when a pattern is found in the corresponding character string.
grep(), which returns a vector of indices of the character strings that contains the pattern.
Both functions need a pattern and an x argument, where pattern is the regular expression you want to match for, and the x argument is the character vector from which matches should be sought.

50
Q

.* are metacharcters , what do they do?

A

., which matches any character (.) zero or more times (). Both the dot and the asterisk are metacharacters. You can use them to match any character between the at-sign and the “.edu” portion of an email address.

grepl(“.@.\.edu” , emails)

51
Q

dedicated packages for time in R

A

lubricade , zoo, xts

52
Q

what are date and time object?

A

In R, dates are represented by Date objects, while times are represented by POSIXct objects. Under the hood, however, these dates and times are simple numerical values. Date objects store the number of days since the 1st of January in 1970. POSIXct objects on the other hand, store the number of seconds since the 1st of January in 1970.

The 1st of January in 1970 is the common origin for representing times and dates in a wide range of programming languages. There is no particular reason for this; it is a simple convention. Of course, it’s also possible to create dates and times before 1970; the corresponding numerical values are simply negative in this case.

53
Q

a walk in the park

That project was a walk in the park compared to this one

I’ve been running marathons for years now, so this 5K run will be a walk in the park for me

We succeeded, but it was not a walk in the park for any of us.

A

something that is very easy to accomplish.
“as any director will tell you, doing Shakespeare isn’t a walk in the park”

A task or activity that is easy or effortless to accomplish.

Something that is easy to do or accomplish.