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)]