# R Basics: Quick and Easy

R is a free and powerful statistical software for analyzing and visualizing data. In this chapter, we provide a quick and easy introduction to R programming.

Read more: What’is R and why learning R?

• Install R and RStudio on windows
• Install R and RStudio for MAC OSX
• Install R and RStudio on Linux  Read more: Installing R and RStudio

• Use R outside RStudio
• Use R inside RStudio
• Launch RStudio under Windows, MAC OSX and Linux
• Set up your working directory
• Set up a default working directory
• Functions: setwd(), getwd() • Basic arithmetic operations: + (addition), - (subtraction), * (multiplication), / (division), ^ (exponentiation)
``````7 + 4 # => 11
7 - 4 # => 3
7 / 2 # => 3.5
7 * 2 # => 14``````

• Basic arithmetic functions:
• Logarithms and exponentials: log2(x), log10(x), exp(x)
• Trigonometric functions: cos(x), sin(x), tan(x), acos(x), asin(x), atan(x)
• Other mathematical functions: abs(x): absolute value; sqrt(x): square root.
``````log2(4) # => 2
abs(-4) # => 4
sqrt(4) # => 2``````

• Assigning values to variables:
``lemon_price <- 2``

• Basic data types: numeric, character and logical
``````my_age <- 28 # Numeric variable
my_name <- "Nicolas" # Character variable
#  Are you a data scientist?: (yes/no) <=> (TRUE/FALSE)
is_datascientist <- TRUE # logical variable``````

• Vectors: a combination of multiple values (numeric, character or logical)
• Create a vector: c() for concatenate
• Case of missing values: NA (not available) and NaN (not a number)
• Get a subset of a vector: my_vector[i] to get the ith element
• Calculations with vectors: max(x), min(x), range(x), length(x), sum(x), mean(x), prod(x): product of the elements in x, sd(x): standard deviation, var(x): variance, sort(x)
``````# Create a numeric vector
friend_ages <- c(27, 25, 29, 26)
mean(friend_ages) # => 26.75
max(friend_ages) # => 29``````

• Matrices: like an Excel sheet containing multiple rows and columns. Combination of multiple vectors with the same types (numeric, character or logical).
• Create and naming matrix: matrix(), cbind(), rbind(), rownames(), colnames()
• Check and convert: is.matrix(), as.matrix()
• Transpose a matrix: t()
• Dimensions of a matrix: ncol(), nrow(), dim()
• Get a subset of a matrix: my_data[row, col]
• Calculations with numeric matrices: rowSums(), colSums(), rowMeans(), colMeans(), apply()
``````     col1 col2 col3
row1    5    2    7
row2    6    4    3
row3    7    5    4
row4    8    9    8
row5    9    8    7``````

• Factors: grouping variables in your data
• Create a factor: factor(), levels()
• Check and convert: is.factor(x), as.factor(x)
• Calculations with factors:
• Number of elements in each category: summary(), table()
• Compute some statistics by groups (for example, mean by groups): tapply()
``````# Create a factor
friend_groups <- factor(c("grp1", "grp2", "grp1", "grp2"))
levels(friend_groups) # => "grp1", "grp2"``````
`` "grp1" "grp2"``
``````# Compute the mean age by groups
friend_ages <- c(27, 25, 29, 26)
tapply(friend_ages, friend_groups, mean)``````
``````grp1 grp2
28.0 25.5 ``````

• Data frames: like a matrix but can have columns with different types
• Create a data frame: data.frame()
• Check and convert: is.data.frame(), as.data.frame()
• Transpose a data frame: t()
• Subset a data frame: my_data[row, col], subset(), attach() and detach()
• Extend a data frame: \$, cbind(), rbind()
• Calculations with numeric data frames: rowSums(), colSums(), rowMeans(), colMeans(), apply()
``````     name age height married
1 Nicolas  27    180    TRUE
2 Thierry  25    170   FALSE
3 Bernard  29    185    TRUE
4  Jerome  26    169    TRUE``````

• Lists: collection of objects, which can be vectors, matrices, data frames,
• Create a list: list()
• Subset a list
• Extend a list
``````my_family <- list(
mother = "Veronique",
father = "Michel",
sisters = c("Alicia", "Monica"),
sister_age = c(12, 22)
)
# Print
my_family``````
``````\$mother
 "Veronique"
\$father
 "Michel"
\$sisters
 "Alicia" "Monica"
\$sister_age
 12 22``````

• Getting help on a specific function: help(mean), example(mean)
• General help about R: help_start()
• Others functions: apropos() and help.search()

• What is R packages?

• Installing R packages
• Install a package from CRAN: install.packages()
• Install a package from Bioconductor: biocLite()
• Install a package from GitHub: devtools::install_github()
• View the list of installed packages: installed.packages()
• Folder containing installed packages: .libPaths()
• Load and use an R package: library()

• View loaded R packages: search()

• Remove installed packages: remove.packages()

• Update installed packages: update.packages()

Read more: Installing and using R packages

• Most used R built-in data sets
• mtcars: Motor Trend Car Road Tests
• iris
• ToothGrowth
• PlantGrowth
• USArrests

Read more: R Built-in data sets