R Lecture Week2 Flashcards

(40 cards)

1
Q

What are R objects

A

we are almost never dealing with a single peice of data, single number, word etc.
we are usually dealing with collections of things
R Objects are the way to combine our individual data in larger structures. The following are all types of R Objects
Arrays
Matrix
Data Frames

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

What is a Variable
how to create a variable

A

Named Values which can change
it allows you to define a value once and reuse it multiple times
3 things are needed when creating a variable
- Variable Name
- Value
- Assignment (arrow)
variable name (- arrow) value

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

which makes good variable name

A

num_classes
num_students
instructor
(informative but succinct)

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

What are the major Data Types to be aware of and their class

A

class(Numeric)

Double (real number with decimals) or Integer (whole numbers[uses less data])

class(character)

character (“text numbers characters”)

class( logical)

Logical/boolean data ( 5 > 6 FALSE) only true or false. as an option

Complex ( imaginary numbers sqrt(-1)). need to use 1i to be sqrt(-1)

Raw

Special Values: NULL, NA, Inf, NaN ( 0/0 = NaN not a number)
NA not avalible, NULL empty

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

create a character 52 and assign it the name example_chr

check its class

change the data type from a character to integer

(explicit coercion)

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

how to convert between the data types

A

as. character
as. logical
as. numeric

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

what is implicit coercion and give an example

A

R assigning a value it can to perform the operation

takes the boolean TRUE to be 1 as its used in a numerical operation

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

What are Operators and what are the types of operators

A

Numeric: * / + - %%

very strict must do math with numbers

“hello” * 5 error(non-numeric argument to binary operator)

Relational: ==, , =>

less strict can compare characters/numeric

“abc” > “xyz” FALSE “hello”> “goodbye” TRUE compare alphabetical order. Logical output (T/F)

Boolean: &, |

Other

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

what is the difference

& |

A

& requires both conditions are met

requires one or the other to be met

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

how to prompt a message and user input

A

readline(prompt = “ask something”)

remember if you were asking for a number it will be saved as a word, we need to use
as.numeric(variable)
to save convert into a number

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

how to glue together a seres of vectors and words into an output response

A

print(paste(variable, “miles is equal to”, variable, “kilometres”))

paste is doing implicite conversion as it converts everything to characters

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

how can you clean up the variables in the global environment window

A

use the broom

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

what is a vector

A

collection of one or more elements of the SAME data type

collection: characters, numbers, trues/false but must be all the same data type

make one using , combine c

c(15, 20, 100, 50)

if you mix data types when making a vector R will implicitly force the data to be characters or number

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

create a number vector with the numbers 15, 20, 25, -5, 102
create a character vector with hello, everyone, !

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

how do you access a vector

A

with square brackets []

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

bring up 1st, 3rd, 5th value

A

my_vector[c(1, 3, 5)]

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

what is a data frame

A

in short a collection of vectors

18
Q

what are Factors

A

vectors with order

19
Q

what are arrays and matrix

A

vectors with dimensions

20
Q
##Write a program that takes a vector of the numbers 1 to 200 in a variable 
##called numbers and prints out a vector that replaces the numbers with values
##of TRUE or FALSE depending upon whether the numbers are even or not. The 
## modulus operator %% may be helpful here.

there are 2 ways

21
Q

Explain Factors

A

allows you to order character variables in a way that is not just alphabetical.

“large” > “medium” > “small” > “very small”

22
Q

create a vector called size,
give the characters, small, med, large

turn it into a factor

specify the levels of the factor

23
Q

what is a list

A

collection of elements of potentially different data types

(can be arrays, matrix, lists in lists even)

24
Q

how to look at data in a list

A

double square brackets.

(part_list [[ 1] ] ) [6:7]

takes the first chunk of data of the object grabs from 6 to 7 in that

25
how to generate random numbers, say 5 random numbers between 1 and 10
sample(1:10, 5)
26
if extracting data from a list what is the difference between example[1] example[[1]]
example[1] #gives list back with only first element example[[1]] #Extracts first element directly
27
what is a matrix
just like a vector except you have rows and columns (2 dimensions) it has data of all the one type the fact it only allows for 1 data type leads itself to maths applications more so than bis data sets
28
make a matrix with 8 rows and 40 pieces of data
29
what is an array
similar to a matrix buy now we have 3 dimensions not 2
30
what is a data frame
what we more commonly use, it has rows and columns within each column = same data type each column can be a new data type
31
what is an object an how can you look into the structure or class of an object
object is the overaching name given to: * **vectors (1)** * **data frames (1)** * matrix * arrays * **lists (2)** * **factors (3)** ``` to look into the structure/class of an object: str or class str(my\_list) ```
32
how to calculate the average
mean( vector) summary(vector) then look at data
33
how to see repeated values in a data frame or vector only once
unique( vector), distinct( vector)
34
how to see the data set in excel type view
view( vector)
35
how to findout how many rows are in a data set
nrow( vector)
36
what are 3 ways to find flights called AA in a data set 1hr 25min
37
how to check if you have NA in your data
anyNA(vector)
38
calculate mean with NA in results
mean( vector, na.rm =TRUE)
39
how can you view the data sets in a package
data(package = “package name”)
40
sort into numerical or alphabetic order
sort( vector