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.





  1. What’is R and why learning R?

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

  1. Installing R and RStudio
  • Install R and RStudio on windows
  • Install R and RStudio for MAC OSX
  • Install R and RStudio on Linux

R logo Rstudio logo

Read more: Installing R and RStudio

  1. Running RStudio and setting up your working directory
  • Use R outside RStudio
  • Use R inside RStudio
    • Launch RStudio under Windows, MAC OSX and Linux
    • Set up your working directory
      • Change your working directory
      • Set up a default working directory
  • Close your R/RStudio session
  • Functions: setwd(), getwd()

RStudio

Read more: Running RStudio and setting up your working directory

  1. R programming basics
  • 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"
[1] "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
[1] "Veronique"
$father
[1] "Michel"
$sisters
[1] "Alicia" "Monica"
$sister_age
[1] 12 22

Read more: R programming basics

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

Read more: Getting help with functions in R programming


  1. Installing and using R packages
  • 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()

  • Unload an R package: detach(pkg_name, unload = TRUE)

  • Remove installed packages: remove.packages()

  • Update installed packages: update.packages()

Read more: Installing and using R packages


  1. R Built-in data sets
  • List of pre-loaded data
  • Loading a built-in R data
  • Most used R built-in data sets
    • mtcars: Motor Trend Car Road Tests
    • iris
    • ToothGrowth
    • PlantGrowth
    • USArrests

Read more: R Built-in data sets










Want to Learn More on R Programming and Data Science?

Follow us by Email

by FeedBurner

On Social Networks:


 Get involved :
  Click to follow us on and Google+ :   
  Comment this article by clicking on "Discussion" button (top-right position of this page)
  Sign up as a member and post news and articles on STHDA web site.


Articles contained by this category :  

Easy R Programming Basics
Getting Help With Functions In R Programming
Installing and Using R Packages
Installing R and RStudio - Easy R Programming
R Built-in Data Sets
Running RStudio and Setting Up Your Working Directory - Easy R Programming
What is R and Why Learning R Programming
This page has been seen 9932 times