R Basics Flashcards
What is the key binding on Max to create a new R script?
command + shift + N
how to install a package in R? how to load the package into R?
install.packages(“dslabs”)
library (dslabs)
how to install two packages at the same time?
install.packages(c(“dslabs”, “tidyverse”))
how to install packages in R studio?
Tools –> install pacages –> type in the names
how to see what packages you have installed in R?
installed.packages()
key short cut on Mac for naming a new script or save in R studio
cmd + s
the key binding on Mac to run an entire R script
cmd + shift + return
the key binding on Mac to run a single R script
cmd + return
how to assign a value to a variable?
using the assignment symbol sign “
how to see the value stored in a variable?
- use function “print(xx)” in R then enter
here xx is the name of the variable - type the name of the variable in R then hit enter
what are objects in R? how to show the names of the defined objects in workspace?
Objects are things are stored in named containers in R. they can be variables, functions, etc.
ls() function can show the names of the objects saved in the workspace.
how to get a square root of a number in R?
sqrt(xx)
how to get more explanation about a function?
- type “help(“xx”)” i.e. help(“log”)
2. directly type “?log” in R. don’t need to use quote here.
how to show the arguments of a variable? i.e. the one for log function.
args(log)
then you will get “(x, base=exp(1))”, meaning the base for log is set up as 1, to change the base, we can further type “log(8, base=2)”, then we get the result 3.
we can also type log(8,2) or log (x=8, base=2). we will get the same results.
what are the rules of naming an object?
it has to begin with a letter
it cannot contain a space
if we want to add some comments in script but not to disrupt the logics of coding, what sign we can use to start the comment?
# this sign is not pre-defined by R in any function, so we can use ## to start a reminder note for ourselves in script.
what sign is used to specify an argument?
equal sign =
i.e. log (x=8, base=2)
what does str function mean?
“str” means” structure”
how to access data frame?
use either str or head function
how to access data from columns of a data frame?
using $ sign, which is called accessor. i.e. mvt$Population - here mvt is the name of the dataframe, population is the name of the column
or user [[]], i.e. mvt[[“population”]]
how to know the names of columns of the data frames?
names ()
how to know how many numeric data points for a column?
length()
i.e. length(mvt$population)
what function can help us know if the vector is numeric or character?
class()
if it’s character, it will return “character”
what is vector? how many types of vectors in R?
vector is an object consisting of several entries and can be numeric, character, logical (true or false), factor (categorical) vectors.