R Basics Flashcards

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

What is the key binding on Max to create a new R script?

A

command + shift + N

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

how to install a package in R? how to load the package into R?

A

install.packages(“dslabs”)

library (dslabs)

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

how to install two packages at the same time?

A

install.packages(c(“dslabs”, “tidyverse”))

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

how to install packages in R studio?

A

Tools –> install pacages –> type in the names

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

how to see what packages you have installed in R?

A

installed.packages()

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

key short cut on Mac for naming a new script or save in R studio

A

cmd + s

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

the key binding on Mac to run an entire R script

A

cmd + shift + return

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

the key binding on Mac to run a single R script

A

cmd + return

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

how to assign a value to a variable?

A

using the assignment symbol sign “

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

how to see the value stored in a variable?

A
  1. use function “print(xx)” in R then enter
    here xx is the name of the variable
  2. type the name of the variable in R then hit enter
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

what are objects in R? how to show the names of the defined objects in workspace?

A

Objects are things are stored in named containers in R. they can be variables, functions, etc.
ls() function can show the names of the objects saved in the workspace.

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

how to get a square root of a number in R?

A

sqrt(xx)

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

how to get more explanation about a function?

A
  1. type “help(“xx”)” i.e. help(“log”)

2. directly type “?log” in R. don’t need to use quote here.

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

how to show the arguments of a variable? i.e. the one for log function.

A

args(log)
then you will get “(x, base=exp(1))”, meaning the base for log is set up as 1, to change the base, we can further type “log(8, base=2)”, then we get the result 3.
we can also type log(8,2) or log (x=8, base=2). we will get the same results.

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

what are the rules of naming an object?

A

it has to begin with a letter

it cannot contain a space

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

if we want to add some comments in script but not to disrupt the logics of coding, what sign we can use to start the comment?

A
#
this sign is not pre-defined by R in any function,  so we can use ## to start a reminder note for ourselves in script.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

what sign is used to specify an argument?

A

equal sign =

i.e. log (x=8, base=2)

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

what does str function mean?

A

“str” means” structure”

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

how to access data frame?

A

use either str or head function

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

how to access data from columns of a data frame?

A

using $ sign, which is called accessor. i.e. mvt$Population - here mvt is the name of the dataframe, population is the name of the column
or user [[]], i.e. mvt[[“population”]]

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

how to know the names of columns of the data frames?

A

names ()

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

how to know how many numeric data points for a column?

A

length()

i.e. length(mvt$population)

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

what function can help us know if the vector is numeric or character?

A

class()

if it’s character, it will return “character”

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

what is vector? how many types of vectors in R?

A

vector is an object consisting of several entries and can be numeric, character, logical (true or false), factor (categorical) vectors.

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

loading the dslabs package and the murders dataset
library(dslabs)
data(murders)

A

determining that the murders dataset is of the “data frame” class
class(murders)
# finding out more about the structure of the object
str(murders)
# showing the first 6 lines of the dataset
head(murders)

26
Q

using the accessor operator to obtain the population column
murders$population
# displaying the variable names in the murders dataset
names(murders)

A
# determining how many entries are in a vector
pop
27
Q
# logical vectors are either TRUE or FALSE
z
A
# factors are another type of class
class(murders$region)
# obtaining the levels of a factor
levels(murders$region)
28
Q

how to use the data of a column to create table?

A
  1. assign the data to a letter i.e. x
29
Q

if a variable type is a factor, we can use nlevel() function to determine the number of levels of a factor.

A

square brackets is useful for subsetting to access specific elements of a vector. i.e. codes [2], codes[c(1,3)], codes[1:2].
if the entries of a vector are named, they may be accessed by referring to their name. i.e. codes [“canada”], codes [c(“egypt”, “italy”)]

30
Q

what are the functions that can create vectors?

A

c()

seq()

31
Q

what is coercion in R?

A

a coercion is an attempt by R to be flexible with data types by guessing what was meant when an entry does not match the expected. i.e.
x

32
Q

what function turns numbers into characters?

A

as.character()

33
Q

what function turns characters into numbers?

A

as.numeric()

34
Q

what functions to associate numeric vectors with character vectors?

A

names(numeric vector)

35
Q

what is the function sort () used for?

A

it sorts s vector in increasing order

36
Q

what is the function order () used for?

A

The function order() produces the indices needed to obtain the sorted vector, e.g. a result of 2 3 1 5 4 means the sorted vector will be produced by listing the 2nd, 3rd, 1st, 5th, and then 4th item of the original vector.

37
Q

what is the function rank () used for?

A

The function rank() gives us the ranks of the items in the original vector.

38
Q

how to get the biggest and smallest value in a vector?

A

The function max() returns the largest value, while which.max() returns the index of the largest value. The functions min() and which.min() work similarly for minimum values.

39
Q

how to find the name of the state with the smallest population in a data frame?

A
  1. pop
40
Q

how to know how many NA in a vector?

A
  1. use is.na() find out how many NA are there

2. define a variable to this i.e. ind

41
Q

how to list a state’s list in a decreasing order of temperature?

A

measure$state[order(measure$temp, decreasing=TRUE)]

here, measure is the file name, state and temp are two columns of data

42
Q

how to get a sum?

A

use sum() function

43
Q

how to create a logical vector that specifies if one variable is less than certain numbers? how to determine the names of the variables that meet this requirements?
how to calculate how many states like this?

A

for example:

index

44
Q

how to create a logical vector that meet multiple logical requirements?

A

use & sign to connect two logical requirements i.e.

west

45
Q

what the use of which() function?

A

it indicates which values are true in a vector i.e.

x

46
Q

how to quickly find the value in a vector?

A

use index() and which i.e. index

47
Q

how to find multiple values in a vector?

A

index

48
Q

how to check if a vector is contained in another vector?

A

%in%

i.e. x

49
Q

which function can change a data table by adding a new column or change the existing one?

A

mutate()

i.e. mutate (object’s name, the target name after mutation)

50
Q

which function can filter the data by subsetting rows?

A

filter()

i.e. filter(murders, rate<0.7)

51
Q

which function can subset the data by selecting specific columns?

A

select ()

i.e. new_table

52
Q

what operator can perform a series of operations by sending the results of one function to another?

A

%>%

i.e. murders %>% select(state,region,rate) %>% filter (rate<0.7)

53
Q

Since data.frame () turns characters into factors. which argument can help avoid turning characters to factors?

A

stringsAsfactors=FALSE

i.e. grades

54
Q

which function gives rank from the lowest to highest?

how to filter the rows ranked 1-5?

A

rank(x)
rank (-x) gives the rank from highest to lowest
filter (object name, rank<=5)

55
Q

how to filter the rows except the ones related to “region of south”? how to count the number of rows?

A

no_south

56
Q

how to filter the rows with a variable meeting two requirements at the same time?

A

using %in% c function i.e.

murders_nw

57
Q

how to filter the rows with variables meeting multiple requirements?

A

filter (object name, condition 1 & condition 2)

58
Q

what is any () function?

A

any () function takes a vector of logicals and returns true if any of the entries are true

59
Q

what is all () function?

A

all() function takes a vector of logicals and returns true if all of the entries are true

60
Q

how to use if_else function?

A

if (condition) {perform some expression} else { alternative expression}

it can be also written as ifelse(condition, expression, alternative expression)

61
Q

How to check if two values are the same?

A

use identical (x,y) function

62
Q

how to define a new function that is unique to you to use?

A

use function() in format function(VARIABLE_NAME){
perform operations on VARIABLE_NAME and calculate VALUE
VALUE
}
i.e.
avg