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
- Change your working directory
- Set up a default working directory
- Close your R/RStudio session
- Functions: setwd(), getwd()
Read more: Running RStudio and setting up your working directory
- 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
- 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
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
- 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
Show me some love with the like buttons below... Thank you and please don't forget to share and comment below!!
Montrez-moi un peu d'amour avec les like ci-dessous ... Merci et n'oubliez pas, s'il vous plaît, de partager et de commenter ci-dessous!
Recommended for You!
Recommended for you
This section contains best data science and self-development resources to help you on your path.
Coursera - Online Courses and Specialization
Data science
- Course: Machine Learning: Master the Fundamentals by Standford
- Specialization: Data Science by Johns Hopkins University
- Specialization: Python for Everybody by University of Michigan
- Courses: Build Skills for a Top Job in any Industry by Coursera
- Specialization: Master Machine Learning Fundamentals by University of Washington
- Specialization: Statistics with R by Duke University
- Specialization: Software Development in R by Johns Hopkins University
- Specialization: Genomic Data Science by Johns Hopkins University
Popular Courses Launched in 2020
- Google IT Automation with Python by Google
- AI for Medicine by deeplearning.ai
- Epidemiology in Public Health Practice by Johns Hopkins University
- AWS Fundamentals by Amazon Web Services
Trending Courses
- The Science of Well-Being by Yale University
- Google IT Support Professional by Google
- Python for Everybody by University of Michigan
- IBM Data Science Professional Certificate by IBM
- Business Foundations by University of Pennsylvania
- Introduction to Psychology by Yale University
- Excel Skills for Business by Macquarie University
- Psychological First Aid by Johns Hopkins University
- Graphic Design by Cal Arts
Books - Data Science
Our Books
- Practical Guide to Cluster Analysis in R by A. Kassambara (Datanovia)
- Practical Guide To Principal Component Methods in R by A. Kassambara (Datanovia)
- Machine Learning Essentials: Practical Guide in R by A. Kassambara (Datanovia)
- R Graphics Essentials for Great Data Visualization by A. Kassambara (Datanovia)
- GGPlot2 Essentials for Great Data Visualization in R by A. Kassambara (Datanovia)
- Network Analysis and Visualization in R by A. Kassambara (Datanovia)
- Practical Statistics in R for Comparing Groups: Numerical Variables by A. Kassambara (Datanovia)
- Inter-Rater Reliability Essentials: Practical Guide in R by A. Kassambara (Datanovia)
Others
- R for Data Science: Import, Tidy, Transform, Visualize, and Model Data by Hadley Wickham & Garrett Grolemund
- Hands-On Machine Learning with Scikit-Learn, Keras, and TensorFlow: Concepts, Tools, and Techniques to Build Intelligent Systems by Aurelien Géron
- Practical Statistics for Data Scientists: 50 Essential Concepts by Peter Bruce & Andrew Bruce
- Hands-On Programming with R: Write Your Own Functions And Simulations by Garrett Grolemund & Hadley Wickham
- An Introduction to Statistical Learning: with Applications in R by Gareth James et al.
- Deep Learning with R by François Chollet & J.J. Allaire
- Deep Learning with Python by François Chollet
Click to follow us on Facebook and Google+ :
Comment this article by clicking on "Discussion" button (top-right position of this page)
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