Basic concepts Flashcards
When was R created and which language inspired it?
R was created in 1996 and was inspired by S Language.
What’s the main purpose of R?
It’s an statistical environment for data analysis and graphs creation.
What are the 3 main characteristics of R?
- Free and open source
- Intepreted language (instead of compiled)
- Object oriented (everything is an object in R)
What’s the current version of R? (as of Feb 2021)
4.0.4
What are the 4 possible IDEs mentioned by Dr. Fernando in the class?
- R Studio Desktop
- R Studio Cloud (create an account first)
- Google Collab
- Emacs + ESS (highly recommended)
What is the Working Directory? When do I need to set it? How to set it?
It’s the folder to which R will be redirected. All imported and exported files will be in this directory. It’s very important to set it before I start working.
I can set it with the function setwd(“/home/paulojardim/pasta”)
I can get it with the function getwd( )
What function do I use to list all the objects created in an environement?
ls( )
It returns a vector of character strings giving the name of the objects.
What is R Workspace?
It’s the place in memory where the variables (objects) are saved. It’s all that was created during a sessios, saved in RAM memory. We can save it in a .Rdata file if they were produced after a long calculation. But the ideal is saving the code itself.
What function should I use to generate random numbers of a uniform distribution? What are its arguments and which are mandatory? What’s the default for the not mandatory?
runif (n, min = 0, max = 1)
n = number of observations we want to return. It’s the mandatory argument
min and max are the limits, they are not mandatory and assume 0 and 1 if not provided.
e.g. runif(5) returns 5 random numbers between 0 and 1
Do I need to name the arguments when I call a funtion in R? What about order?
No, I don’t neeed to name them. But if I don’t name them I need to respect the order. If I name them I can use in any order.
How can I easily see the arguments of a function?
I can call args( ) function.
e.g. args( sample ) returns:
function (x, size, replace = FALSE, prob = NULL)
How do I know what are the mandatory arguments of a function?
When I see the args of a function, the ones that don’t have a default value are mandatory:
e.g. in sample() function below, x and size are mandatory
function (x, size, replace = FALSE, prob = NULL)
What is the techincal name of “…” and when should I use it in my function?
It’s called ellipsis and I use basically in two situations:
- When it makes sense for a function receiving an undefined number of arguments (e.g. print function). Then I can transform the arguments in a list:
- arguments = list(…)*
2. When I need to receive arguments to pass to a generic function.
What function should I use to concatenate strings?
paste(“string”, “string2”, “string3” , …… )
How can I easily see the documentation of a function?
I can use ?function or help(function).
They both return the same thing
Within the documentation, what’s the session that tells me about what the function returns?
The Value session.
What function returns a list of functions/objects containing a expression?
apropos(“mod”)
What function returns a list of functions containing a word in any part of their documentation?
help.search(“geo”)
How can I see what are the loaded/attached packages at the moment and what are their code paths in my computer?
- search()* - lists the loaded/attached packages
- searchpaths()* - lists their paths in my computer
What is the most basic way of getting access to R official documentation?
- Run R from the terminal by executing “R” command
- In R prompt, execute help.start( ). It will launch a local webserver and open the html manuals and documentation.
Introduction to R and The R Language Definition are the main ones.
Worthy reading!
What are the 7 packages that are loaded/attached automatically when we run R?
- base
- utils
- stats
- graphics
- grDevices
- datasets
- methods
How do I load an installed package in R?
I need to run function library() providing the name of the package.
How do I install a new package in R?
Run function install.packages(“package_name”)
How do I verify if the installed packages need updates?
Execute function packageStatus()
How do I update all installed packages automatically?
Run function update.packages(ask = FALSE)
How do I create a simple function in R?
helloWorld = function( ) {
writeLines(“Hello”)
}
it’s preferable using the arrow instead of equal sign but Brainscape bugs with arrow
How can I delete one object of my workspace? And how can I delete all objects?
rm(x)
rm(list = ls( ) )
What’s the meaning of “everything is a vector in R”? Is it bad? What about simple number like 15?
R doesn’t have primitive data types in the way that other languages do. In R even the simplest numeric value is an example of a vector.
This might seems like a crazy idea and potentially inefficient, but it fits in well with the sort of calculations you want to do in R.
A number occurring by itself in an expression (e.g. 15) is taken as a vector of length one.
Why R console prints [1] 5 when I type 5?
Because 5 is also a vector (everything in R is). It’s a vector of length one. The [1] means that the console is printing the first element of the vector. When I print a big vector, each row has the number corresponding to the index of the first vector element in that row.
What are the two types of vectors and their subtypes? What’s the main difference between those two types?
Atomic Vectors and Lists.
Atomic vectors have six types:
- double
- integer
- character
- logical
- complex
- raw
Lists are also a vector but they can contain more than one datatype. That’s their main difference.
What’s the difference between type and class?
Complex data structures are created based on atomic vectors. When they are created we have a class. There are thousands of classes. One object can be of any of these clasess but their type will always be one of the six vector types (or a list)
How do I check the type of an object? And its class?
I use typeof( ) function to check the type.
And I use class( ) function to check the class?
Whats the type of x? And its class?
x = c(2, 4, 6)
type: double
class: numeric
What’s the type of x? And its class?
x = c(2L, 4L, 6L)
Type: integer
Class: integer
What’s the type of x? And its class?
x = c(“a”, “b”, “c”)
Type: character
Class: character
What’s the type of x? And its class?
x = c(TRUE, FALSE, TRUE)
Type: logical
Class: logical
Whats the type of x? And its class?
x = c(2 + 1i, 4 + 1i, 6 + 1i)
Type: complex
Class: complex
What’s the type of x? And its class?
x = raw(3)
Type: raw
Class: raw
What function do I use to create new vectors?
c( )
Concatenate function.
Does R understand a number like 5 as an integer? What do I need to do to accomplish it? Is there a difference in termos of memory usage?
No, it understands and stores as a double. If I want it to take the number as integer I need to use the sufix L:
5L
Yes, there is a difference in terms of memory usage because double numbers require more space.
What function to I use to see an estimate of the space in memory that is being used to store an R object? What package do I need to load?
function object.size(myobject)
I don’t need to load any package because it’s in utils package which is pre loaded.
What function do I use to generate a sequence? What are its arguments?
seq ( )
seq(from = 1, to = 1, by = ((to - from)/(length.out - 1)),
length.out = NULL, along.with = NULL, …)
- length.out - is the total number of elements I want
- along.with* - take the length from the length of this argument.
What’s the difference between:
rep(1:4, 2)
and rep(1:4, each = 2)
- rep(1:4, 2) returns “1 2 3 4 1 2 3 4”
- rep(1:4, each = 2) returns “1 1 2 2 3 3 4 4”
Can I do math operations between a vector and a number? What’s the result of c(3,4,5) * 2 ?
Yes! Remember that numbers ARE vectors of lenght 1. The result is “6 8 10”
We can do math operations with vectors of the same legth or of multiple length.
What’s the recycling rule and how does it work?
It’s the way R behaves when you do arithmetic operations with two vectors of different sizes.
The shortest vector is concatenated to itself till it’s length is the same as the longer vector. Then R does the operation.
It only works if the longer object legth is multiple of the shorter object length.
e.g. c(1, 2, 3) * c(4, 5, 6, 7, 8, 9) is actually:
c(1, 2, 3, 1, 2, 3) * c(4, 5, 6, 7, 8, 9)
What is the type of z? What are the values of it?
num = c(2, 4, 5, 6)
z = num > 4
z is a logical vector: FALSE FALSE TRUE TRUE