R Basic Flashcards

Learn basics of R

1
Q

What does IDE stand for?

A

Interactive Development Environment

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

FUNCTION: install a package in R

A

install.packages (“packagename”)

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

FUNCTION: load and attach an R Package to a session

A

Library (packagename)

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

FUNCTION: List of objects

A

ls()

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

FUNCTION: square root

A

sqrt

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

FUNCTION: find the object data type

A

class(object)

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

What is a data frame?

A

a table

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

FUNCTION: load a data set

A

data(“data set name”)

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

What is the $ called?

A

the accessor

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

What is an accessor?

A

a function that allows a user to interact with slots of an object

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

== is what

A

logical operator, will be true or false

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

what is data type “factor”?

A

categorical data

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

FUNCTION: levels()

A

returns categories/options from a “factor data” type object

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

FUNCTION: names()

A

designed to extract the column names from a data frame
Can also be used to assign names to numeric values
ie.
cost

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

R name for “data type”

A

class

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

FUNCTION: table()

A

takes a vector as input and returns the frequency of each unique element in the vector

> x table(x)
x
a b c
2 3 1

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

FUNCTION: to concatenate

A

c()

ie. c(a,b,c)
c(a=1,b=2,c=3)

18
Q

FUNCTION: to access a subset/column of a vector

A

VectorName[2]
##the 2 is for the second column
VectorName[c(1,3)]
VectorName[“columnname”,”columnname2”]

19
Q

When creating a vector, what indicates that a element is a string, not a variable?

A

Double quotes around the element (for strings)

20
Q

What is coercion?

A

An attempt by R to be flexible with data types

21
Q

FUNCTION: to convert variable/object to character types

A

as.character()

22
Q

FUNCTION: to convert variable/object to number types

A

as.numeric()

23
Q

What does NA mean?

A

Number missing

Can be created by R trying to coerce data

24
Q

FUNCTION: to specify a length

A

length. out=5

ie. seq(0, 100, length.out = 5)

25
FUNCTION: returns the sorted indexes from a vector
order(x) This is the index that puts x in order ``` EXAMPLE > x sort(x) [1] 4 15 31 65 92 > order(x) [1] 2 3 1 5 4 > index x[index] [1] 4 15 31 65 92 ```
26
FUNCTION: that returns index of max/min from vector
which. max() | which. min()
27
FUNCTION: which()
returns the indexes for the values which are true
28
FUNCTION: match()
looks for entries in a vector and returns the index needed to access them EXAMPLE >index index [1] 33 10 44
29
FUNCTION: %in%
indicates whether or not an element of a first vector is in a second vector EXAMPLE > x y y %in% x [1] TRUE, TRUE, FALSE
30
FUNCTION: mutate() | deplyr
to change a table ie. murders
31
FUNCTION: filter() | dplyr
filter(murders,rate <= 0.71)
32
FUNCTION: %>% | dplyr
Passes object on left hand side as first argument (or . argument) of function on righthand side x %>% f(y)is the same as f(x, y)y %>% f(x, ., z)is the same as f(x, y, z )
33
When creating a data.frame, how do you prevent every element from being classified as a factor data type?
End the function with stringsAsFactors=FALSE
34
FUNCTION: what function to create scatterplot
plot(x,y)
35
FUNCTION: to create a historgram
hist(x,y)
36
FUNCTION: to create boxplot
boxplot(x,y)
37
General Form for "IF - ELSE" statement
``` if (boolean condition) { expressions } else { alternative expressions } ```
38
FUNCTION: ifelse()
ifelse(if true, true, false) Can use this replace values in a vector ifelse(is.na(na_example), 0, na_example)
39
FUNCTION: any()
if ANY entries are TRUE, will return TRUE
40
FUNCTION: all()
if ALL entries are TRUE, will return TRUE
41
HOW TO CREATE A FUNCTION
my_function_name
42
FUNCTION: For loop
for(i in 1:5){ print(i) } ``` [1] 1 [1] 2 [1] 3 [1] 4 [1] 5 ``` EXAMPLE: m