R Lecture Week2 Flashcards
What are R objects
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
What is a Variable
how to create a variable
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
which makes good variable name
num_classes
num_students
instructor
(informative but succinct)
What are the major Data Types to be aware of and their class
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
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)
how to convert between the data types
as. character
as. logical
as. numeric
what is implicit coercion and give an example
R assigning a value it can to perform the operation
takes the boolean TRUE to be 1 as its used in a numerical operation
What are Operators and what are the types of operators
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
what is the difference
& |
& requires both conditions are met
requires one or the other to be met
how to prompt a message and user input
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 to glue together a seres of vectors and words into an output response
print(paste(variable, “miles is equal to”, variable, “kilometres”))
paste is doing implicite conversion as it converts everything to characters
how can you clean up the variables in the global environment window
use the broom
what is a vector
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
create a number vector with the numbers 15, 20, 25, -5, 102
create a character vector with hello, everyone, !
how do you access a vector
with square brackets []
bring up 1st, 3rd, 5th value
my_vector[c(1, 3, 5)]
what is a data frame
in short a collection of vectors
what are Factors
vectors with order
what are arrays and matrix
vectors with dimensions
##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
Explain Factors
allows you to order character variables in a way that is not just alphabetical.
“large” > “medium” > “small” > “very small”
create a vector called size,
give the characters, small, med, large
turn it into a factor
specify the levels of the factor
what is a list
collection of elements of potentially different data types
(can be arrays, matrix, lists in lists even)
how to look at data in a list
double square brackets.
(part_list [[ 1] ] ) [6:7]
takes the first chunk of data of the object grabs from 6 to 7 in that
how to generate random numbers, say 5 random numbers between 1 and 10
sample(1:10, 5)
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
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
make a matrix with 8 rows and 40 pieces of data
what is an array
similar to a matrix buy now we have 3 dimensions not 2
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
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)
how to calculate the average
mean( vector)
summary(vector) then look at data
how to see repeated values in a data frame or vector only once
unique( vector), distinct( vector)
how to see the data set in excel type view
view( vector)
how to findout how many rows are in a data set
nrow( vector)
what are 3 ways to find flights called AA in a data set
1hr 25min
how to check if you have NA in your data
anyNA(vector)
calculate mean with NA in results
mean( vector, na.rm =TRUE)
how can you view the data sets in a package
data(package = “package name”)
sort into numerical or alphabetic order
sort( vector