R Flashcards

1
Q

assign a value to a variable

A

use

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

determining that the object (murders dataset) is of the “data frame” class

A

class(murders)

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

finding out more about the structure of the object

A

str(murders)

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

showing the first 6 lines of the dataset

A

head(murders)

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

obtain a specific column (ex population)

A

murders$population

$ is called accessor operator

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

displaying the variable names in the murders dataset

A

names(murders)

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

determining how many entries are in a vector

A

pop

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

obtaining the levels of a factor

A

levels(murders$region)

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

obtaining the number of levels of a factor

A

nlevels()

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

create vectors of class numeric or character

A

use “concentrate function”

ex: codes

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

name the elements of a numeric vector

A

codes

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

name the elements of a numeric vector in a different way

A

codes

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

access specific elements of a vector

A

Using square brackets

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

The function ____ sorts a vector in increasing order.

A

sort()

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

The function _____ produces the indices needed to obtain the sorted vector

A

order()

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

The function ____ gives us the ranks of the items in the original vector

A

rank()

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

The function ____ returns the largest/smallest value

A

max() and min()

18
Q

The function ____ returns the index of the largest/smallest value

A

which.max() and which.min()

19
Q

The name of the state with the maximum population is found by doing ??

A

murders$state[which.max(murders$population)]

20
Q

how to obtain the murder rate

A

murder_rate

21
Q

ordering the states by murder rate, in decreasing order

A

murders$state[order(murder_rate, decreasing=TRUE)]

22
Q

Creating a logical vector that specifies if the murder rate in that state is less than or equal to 0.71

A

index

23
Q

Determining which states have murder rates less than or equal to 0.71

A

murders$state[index]

24
Q

Calculating how many states have a murder rate less than or equal to 0.71

A

sum(index)

25
Q

efining an index and identifying states with both conditions true

A
# creating the two logical vectors representing our conditions
west
26
Q

The function _______ gives us the entries of a logical vector that are true.

A

which()

27
Q

The function _______ looks for entries in a vector and returns the index needed to access them.

A

match()

28
Q

if we want to know whether or not each element of a first vector is in a second vector

A

function %in%

29
Q

Change a data table by adding a new column, or changing an existing one

A

dplyr package:

mutate() function

30
Q

To filter the data by subsetting rows

A
dplyr package:
function filter()
31
Q

To subset the data by selecting specific columns

A

dplyr package:

select() function

32
Q

To perform a series of operations by sending the results of one function to another function

A

using the pipe operator, %>%.

murders %>% select(state, region, rate) %>% filter(rate <= 0.71)

33
Q

create data frames

A

data.frame()
By default, the data.frame() function turns characters into factors. To avoid this, we utilize the stringsAsFactors argument and set it equal to false.

34
Q
create a simple scatterplot 
# a simple scatterplot of total murders versus population
A
function plot()
x
35
Q

Histograms. Ex: a histogram of murder rates

A

hist()

hist(murders$rate)

36
Q

Boxplots. Ex: the rate per region in the murders dataset

A

boxplot()

boxplot(rate~region, data = murders)

37
Q

works on vectors by examining each element of the vector and returning a corresponding answer accordingly.

A

ifelse() function

38
Q

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

A

any()

39
Q

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

A

all()

40
Q

define a new function

A

function()

41
Q

general form of a for-loop

A

For i in [some range], do operations
for(i in 1:5){
print(i)