The R cheat sheet provides a comprehensive overview of concepts like variables, data types, operators, conditional statements, loops, functions, and more. It also covers advanced topics like data structures (lists, vectors, matrices, data frames) and data visualization (histograms, bar plots, pie charts), and many more.
R Introduction
R is a popular programming language in the field of data science, known for its powerful tools for statistical computing and data visualization.
You can use R to retrieve data from datasets, analyze and visualize them, and present your findings through visually appealing graphics.
Whether you are a beginner or an experienced data scientist, R provides a comprehensive software environment for tackling a wide range of data-related tasks.
Hello World Program in R
# print Hello World on the screen
print("Hello World")
# Output: Hello World
Variables
A variable is a named container that stores a value.
In R, you do not need to specify the data type of a variable when you declare it.
R automatically assigns a data type to the variable based on the value you assign to it.
You can use the =
or <-
operator to assign value to a variable.
# use = to assign value to a variable
age = 24
# use <- to assign value to a variable
number <- 10
We will use the =
operator as it makes the code cleaner.
Data Types
A value can be a number or a text. In programming, we have datatypes to specify types of values. For example, 3 is a numeric value, and "Hi" is a string.
In R, we use class()
function to check the datatypes of a value.
Here are the 6 basic data types in R:
1. logical: stores only two values: TRUE and FALSE. They are also called boolean values.
bool = TRUE
print(class(bool))
# Output: logical
2. numeric: represents all real numbers with decimal values. For example,
number1 = 12
print(class(number1))
# Output: numeric
or without decimal values For example,
number2 = 3.3
print(class(number2))
# Output: numeric
3. integer: represents real numbers without decimal. We use suffix L to specify integer data.
number = 12L
print(class(number))
# Output: integer
4. complex: represents complex numbers. We use suffixes to specify the imaginary part.
z = 3 + 2i
print(class(z))
# Output: complex
5. character: represents character or string values.
letter = "A"
print(class(letter))
# Output: character
text = "Apple"
print(class(text))
# Output: character
6. raw: represents raw byte sequence.
# create a raw vector with 5 bytes
x = raw(5)
print(x)
# Output: 00 00 00 00 00
# Check type using class()
x = raw(5)
print(class(x))
# Output: raw
R Print Output
1. You can print output in R using the print()
function.
Example
number = 10
print(number)
# Output: [1] 10
text = "ten"
print(text)
# Output: [10] ten
The brackets '[1]'
indicates that the output is a vector with a single element.
2. You can also use cat()
function to print output.
Example
number = 10
text = "ten"
cat(number,text)
# Output
# [1] 10ten
Here, the cat()
function concatenates its arguments into a single string and prints the result.
3. You can also print variables as a part of a string using paste()
and sprintf()
functions.
Example of paste()
The paste()
function concatenates string and variables and returns the result as a string.
name = "John"
age = 24
print(paste("My name is", name, "and I am", age, "years old."))
# Output:
# My name is John and I am 24 years old.
Example of sprintf()
The sprintf()
function is similar to paste()
, but it allows you to specify a format for the output string. It uses placeholders %s
for strings and %d
for numbers.
name = "John"
age = 24
sprintf("My name is %s and I am %d years old.", name, age)
# Output:
# My name is John and I am 24 years old.
R Input
1. To read input from the user in R, you can use the readline()
function.
name = readline("Enter your name:")
print("Name:", name)
# Output:
# Enter your name: John
# Name: John
Here, the readline()
function takes input "John" and returns it.
2. The readline()
function reads input from the user as a string.
age = readline("Enter your age: ")
print(class(age))
# Output:
# Enter your age: 25
# <class 'str'>
R Operators
An operator is a special symbol that carries out operations on values and variables. For example, the +
operator performs an addition between two values.
sum = 5 + 4
print(sum) # Output: 9
Different Types of R Operators
- Arithmetic Operators
- Comparison Operators
- Logical Operators
Arithmetic Operators
R supports the following arithmetic operators:
+ (addition): adds two values or variables
a = 52
b = 18
print(a + b) # Output: 70
- (subtraction): subtracts two values or variables
a = 52
b = 18
print(a - b) # Output: 34
* (multiplication): multiple two values or variables
a = 3
b = 5
print(a * b) # Output: 15
/ (division): divides two numbers and returns a floating point result
a = 32
b = 5
print(a / b) # Output: 6.4
** (exponentiation): raises a number to the power of another
a = 3
b = 4
print(a ** b) # Output: 81
Operator Precedence
The arithmetic operators have a defined order of precedence, that specifies which operator will execute first if there exist multiple operators in a single expression. For example,
result = 2 ** 3 + 10 / 5
print(result) # Output: 10
Here, **
is executed first, followed by /
and then +
Order of operator precedence from highest to lowest:
**
*
,/
+
,-
You can use parentheses to change the order of precedence and specify which operations should be performed first.
result = 2 ** (3 + (10 / 5))
print(result) # Output: 32
Here, /
is executed first, followed by +
and then **
Comparison Operators
R supports the following comparison operators:
== (equal to): checks if two values or variables are equal
a = 20
b = 30
print(a == b) # Output: FALSE
!= (not equal to): checks if two values or variables are not equal
a = 20
b = 30
print(a != b) # Output: TRUE
< (less than): checks if the value on the left is less than the value on the right
a = 20
b = 30
print(a < b) # Output: TRUE
<= (less than or equal to): checks if the value on the left is less than or equal to the value on the right
a = 20
b = 30
print(a <= b) # Output: TRUE
> (greater than): checks if the value on the left is greater than the value on the right
a = 20
b = 30
print(a > b) # Output: FALSE
>= (greater than or equal to): checks if the value on the left is greater than the value on the right
a = 20
b = 30
print(a >= b) # Output: FALSE
Logical Operators
R supports the following logical operators:
&& (Logical AND): returns TRUE
if both operands are TRUE
, else returns FALSE
a = 30
b = 20
c = 10
result = (a > b) && (a > c)
print(result) # Output: TRUE
result = (a > b) && (a < c)
print(result) # Output: FALSE
|| (Logical OR): returns TRUE
if at least one operand is TRUE
, else returns FALSE
a = 30
b = 20
c = 10
result = (a < b) || (a < c)
print(result) # Output: FALSE
result = (a > b) || (a < c)
print(result) # Output: TRUE
R Conditional Statements
Conditional statements execute a block of code only if the condition is met. These are the conditional statements in R:
- if
- if...else
- else if
- Nested if...else
if Statement
The if
statement executes a block of code only if a certain condition is True
.
Syntax
if (condition) {
# block of code
}
Example: when condition is TRUE
number = 10
if (number > 0) {
print("Positive number")
}
print("End of if")
# Output:
# Positive number
# End of if
Example: when condition is FALSE
number = -5
if (number > 0) {
print("Positive number")
}
print("End of if")
# Output:
# End of if
if...else Statement
The if...else statement executes a block of code when the condition is True and another block of code when the condition is False.
Syntax
if (condition) {
# code block when condition is TRUE
} else {
# code block when condition is FALSE
}
Example: when condition is TRUE
number = 10
if (number > 0) {
print("Positive number")
} else {
print("Negative number")
}
print("End of if...else")
# Output:
# Positive number
# End of if...else
Example: when condition is FALSE
number = -5
if (number > 0) {
print("Positive number")
} else {
print("Negative number")
}
print("End of if...else")
# Output:
# Negative number
# End of if...else
else if Statement
The else if statement adds additional conditions when we have to make a choice from more than 2 options.
Syntax
if (condition1) {
# code block when condition1 is TRUE
} else if (condition2) {
# code block when condition2 is TRUE
} else if (condition3) {
# code block when condition3 is TRUE
}
....
} else {
# code block when all conditions are FALSE
}
Example: when first condition is True
number = 10
if (number > 0) {
print('Positive Number')
} else if (number < 0) {
print('Negative Number')
} else {
print('Zero Number')
}
# Output: Positive Number
Example: when second condition is TRUE
number = -5
if (number > 0) {
print('Positive Number')
} else if (number < 0) {
print('Negative Number')
} else {
print('Zero Number')
}
# Output: Negative Number
Example: when all conditions are FALSE
number = 0
if (number > 0) {
print('Positive Number')
} else if (number < 0) {
print('Negative Number')
} else {
print('Zero Number')
}
# Output: Zero Number
ifelse() Function
The ifelse()
function in R is a vectorized version of the if...else statement.
Syntax
ifelse(condition, value_if_true, value_if_false)
Example
# input vector
x <- c(12, 9, 23, 14, 20, 1, 5)
# ifelse() function to determine odd/even numbers
ifelse(x %% 2 == 0, "EVEN", "ODD")
# Output: "EVEN" "ODD" "ODD" "EVEN" "EVEN" "ODD" "ODD"
R Loops
Loops allow us to execute a block of code multiple times.
For example, if you want to print a line of text 100 times, rather than typing the same code 100 times, we can use a loop to perform this task.
Types of Loops:
- while Loop
- for Loop
while Loop
The while loop executes a block of code repeatedly as long as the specified condition is TRUE.
Syntax
while (condition) {
# code block to execute if condition is True
}
Example
i = 1
while (i <= 5) {
print(i)
i = i + 1
}
Output
[1] 1 [1] 2 [1] 3 [1] 4 [1] 5
for Loop
The for loop allows us to iterate over a sequence.
Syntax
for (item in sequence) {
# code block to execute
}
Example
numbers = c(1, 2, 3, 4, 5)
# for loop to print all elements in numbers
for (x in numbers) {
print(x)
}
Output
[1] 1 [1] 2 [1] 3 [1] 4 [1] 5
break with Loop
We can use the break
statement to immediately terminate the loop. It is generally used with if...else
statements.
Syntax
while (condition) {
# code to execute
if (condition) {
break
}
}
Example: break with a while loop
numbers = c(2, 3, 12, 14, 5, 19, 23, 64)
for (i in numbers) {
# break the loop if number is 5
if( i == 5) {
break
}
print(i)
}
Output
[1] 2 [1] 3 [1] 12 [1] 14
Example: break with a for loop
i = 0
while (i < 5) {
i = i + 1
# break the loop if number is 3
if (i == 3) {
break
}
print(i)
}
Output
[1] 1 [1] 2
next with Loop
We can use the next
to skip the current iteration of the loop. It is generally used with if...else
statements.
Syntax
while (condition) {
# code to execute
if (condition) {
next
}
}
Example: continue with a while loop
x = c(1, 2, 3, 4, 5, 6, 7, 8)
for(i in x) {
if (i %% 2 != 0) {
next
}
print(i)
}
Output
[1] 2 [1] 4 [1] 6 [1] 8
Example: continue with a for loop
i = 0
while (i < 5) {
i = i + 1
if (i == 3) {
next
}
print(i)
}
Output
[1] 1 [1] 2 [1] 4 [1] 5
Function
Functions help us to divide our program into smaller chunks so that it is easier to use and understand. A function can be used multiple times in the same program.
Syntax
function_name = function(parameters){
statement(s)
}
Example
greet = function(name) {
paste("Hello", name)
}
Here, greet()
is a function name and takes one parameter.
To execute the function, we need to call the function.
Function call
greet("John")
In the function call, we passed the value "John" as a parameter of the function.
Output
If compiled together, the output of the above program will be
Hello John
Example: Add two numbers using function
# create function with two parameters
add_numbers = function(n1, n2) {
sum = n1+n2
paste("sum =", sum)
}
# function call
add_numbers(2, 6)
Output
sum = 8
return statement
A return statement is an optional statement used to exit a function and go back to the place from where it is called.
Example
add_numbers = function(n1, n2) {
sum = n1 + n2
return (sum)
}
# function call
result = add_numbers(2, 6)
print(result)
# Output : 8
Data Structures
In R, data structures include strings, vectors, matrix, list, arrays, data frame, and factors.
String
A string is a sequence of characters. We can represent strings using single or double quotes.
Example
message1 = 'Hello'
print(message1)
message2 = "Welcome to Programiz"
print(message2)
String Operations
There are various built-in functions to perform different operations on strings.
1. nchar(): to find the length of a string.
message = "Programiz"
nchar(message) # Output: 9
2. paste(): to join two or more strings together.
message1 = "Welcome to"
message2 = "Programiz"
paste(message1, message2)
# Output: Welcome to Programiz
3. ==: to compare two strings, returns TRUE if strings are equal, else returns FALSE.
message1 = "Hello, World!"
message2 = "Hallo, Wereld!"
message3 = "Hello, World!"
print(message1 == message2) # FALSE
print(message1 == message3) # TRUE
4. toupper(): to convert string to uppercase.
message = "R Programming"
upper = toupper(message)
print(upper)
# Output: R PROGRAMMING
5. tolower(): to convert string to lowercase.
message = "R Programming"
lower = tolower(message)
print(lower)
# Output: r programming
Vectors
In R, a vector stores data of a similar type. For example, Suppose we need to record the age of 5 employees. Instead of creating 5 separate variables, we can simply create a vector.
age = c(25, 29, 32, 33, 35)
Here are some common vector operations:
1. Create a vector
We use c()
to write elements of vectors.
# create vector of string types
employees = c("John", "Smith", "Ray")
print(employees)
# Output: [1] "John" "Smith" "Ray"
2. Aceess Vector Elements
We can access vector elements using their vector index. In R, the index starts from 1, not 0. Let's see an example.
employees = c("John", "Smith", "Ray")
# access 1st element
print(employees[1]) # "John"
# access 3rd element
print(employees[3]) # "Ray"
Here, we could access the vector element using their respective index.
3. Modify Vector Elements
To change a vector element, we can simply reassign a new value to the specific index.
countries = c("USA", "Canada", "France")
# modify elemenet at index 1
countries[1] = "Mexico"
print(countries)
# Output: "Mexico" "Canada" "France"
List
List is a collection of similar or different types of data. In R, we use list() function to create a list. For example,
random = list(1, "one", TRUE)
Here are some common list operations:
1. Access List Elements
We can access list elements using the index number.
person = list("Charlie", 26, 6.2, "California")
# access 1st item
print(person[1])
# access 3rd item
print(person[3])
Output
[[1]] [1] "Charlie" [[1]] [1] 6.2
2. Modify a List Element
person = list("Charlie", 26, 6.2, "California")
# change 2nd element
person[2] = 28
print(person)
Output
[[1]] [1] "Charlie" [[2]] [1] 28 [[3]] [1] 6.2 [[4]] [1] "California"
3. Remove List Element
We can remove a list element using the index number and adding a negative sign -
to indicate we want to delete the item.
cities = list("California", "Toronto", "Tokyo")
# remove 3rd item
print(cities[-3])
Output
[[1]] [1] "California" [[2]] [1] "Toronto"
4. Name a List Element
We can also name the list element using $
, and access these elements using their names.
# assign name to vector elements
city_temperature = list(city = "California", temperature = 10)
# access element by its name
print(city_temperature$city) # California
Matrix
A matrix is a two-dimensional array of data that is organized into rows and columns. We can create a matrix using the matrix()
function.
Syntax
matrix(vector, nrow, ncol)
Here,
vector
- the data items of same typenrow
- number of rowsncol
- number of columnsbyrow
(optional) - if TRUE, the matrix is filled row-wise. By default, the matrix is filled column-wise.
Example
matrix1 = matrix(c(1,2,3,4,5,6), nrow = 2, ncol = 3)
print(matrix1)
Output
[,1] [,2] [,3] [1,] 1 3 5 [2,] 2 4 6
Here, we created a matrix with 2 rows and 3 columns, and vectors c(1,2,3,4,5,6)
.
Access Matrix Elements
fruit <- matrix(c("apple", "banana", "kiwi", "avocardo"), nrow = 2, ncol = 2)
print(fruit)
# access element at 1st row, 2nd column
cat("\nAccessed Element:", fruit[1, 2])
Output
[,1] [,2] [1,] "apple" "kiwi" [2,] "banana" "avocardo"
Array
Array is similar to a matrix, but it allows us to store data in more than 2 dimensions.
In R, we create an array using the array()
function.
Syntax
array(vector, dim = c(nrow, ncol, nmat))
Here,
vector
- the data items of the same typenrow
- number of rowsncol
- number of columnsnmat
- the number of matrices ofnrow
*ncol
dimension
Example
# create two 2 by 3 matrix
array1 = array(c(1:12), c(2,3,2))
print(array1)
Output
, , 1 [,1] [,2] [,3] [1,] 1 3 5 [2,] 2 4 6 , , 2 [,1] [,2] [,3] [1,] 7 9 11 [2,] 8 10 12
In the above example, notice the arguments we passed in the array()
function.
array(c(1:12), c(2,3,2))
Here,
c(1:12)
- a vector with values from 1 to 12c(2,3,2)
- create two matrices of 2 by 3 dimension
Access Array Elements
We use the []
operator to access specific elements of an array.
Syntax
array[n1, n2, matrix_level]
Here,
n1
- specifies the row positionn2
- specifies the column positionmatrix_level
- specifies the matrix level
Example
# create two 2 by 3 matrix
array1 = array(c(1:12), c(2,3,2))
print(array1)
# access element at 1st row, 3rd column of 2nd matrix
cat("\nAccessed Element:", array1[1, 3, 2])
Output
, , 1 [,1] [,2] [,3] [1,] 1 3 5 [2,] 2 4 6 , , 2 [,1] [,2] [,3] [1,] 7 9 11 [2,] 8 10 12 Accessed Element: 11
In the above example, we have accessed an array element at 1st row, 3rd colomn of 2nd matrix using
array1[1,3,2]
Data Frame
Data frames store data in tabular format. We use data.frame()
function to create data frames.
Syntax
dataframe1 <- data.frame(
first_column = c(val1, val2, ...),
second_column = c(val1, val2, ...),
...
)
Here,
first_column
- a vector with values of same datatypesecond_column
- another vector with values of same datatype, and so on
Example
voters <- data.frame (
Name = c("Juan", "John", "Peter"),
Age = c(25, 15, 19),
Vote = c(TRUE, FALSE, TRUE)
)
print(voters)
Output
Name Age Vote 1 Juan 25 TRUE 2 John 15 FALSE 3 Peter 19 TRUE
Access Data Frame Columns
We can access data frame columns using []
or $
operator. Let's see an example.
# create a data frame with two columns and four rows
student <- data.frame(
name = c("Alice", "Bob", "Charlie", "David"),
score = c(90, 80, 85, 95)
)
# access column 1 element using []
print(student[1])
# access column 1 elements using $
cat(student$name)
Output
name 1 Alice 2 Bob 3 Charlie 4 David Alice Bob Charlie David
Combine Data Frames
We use rbind()
to combine data frames vertically and cbind()
to combine the data frames horizontally.
Example: rbind() to combine data frames vertically
# create the first data frame
df1 <- data.frame(
name = c("Alice", "Bob", "Charlie"),
score = c(70, 60, 80))
# create the second data frame
df2 <- data.frame(
name = c("David", "Eve", "Frank"),
score = c(80, 50, 75))
# combine the two data frames using rbind()
df_combined <- rbind(df1, df2)
print(df_combined)
Output
name score 1 Alice 70 2 Bob 60 3 Charlie 80 4 David 80 5 Eve 50 6 Frank 75
Example: cbind() to combine data frames horizontally
# create the first data frame
df1 <- data.frame(
name = c("Alice", "Bob", "Charlie"),
score = c(70, 60, 80))
# create the second data frame
df2 <- data.frame(
name = c("David", "Eve", "Frank"),
score = c(80, 50, 75))
# combine the two data frames using rbind()
df_combined <- cbind(df1, df2)
print(df_combined)
Output
name score name score 1 Alice 70 David 80 2 Bob 60 Eve 50 3 Charlie 80 Frank 75
R Bar Plot
Bar graph are an efficient way to represent data. R allows us to create a bar plot and visualize data better.
We can create bar plot using the function barplot()
function. For example,
# create a vector of data
data <- c(1, 2, 3, 4, 5)
# create a bar plot
barplot(data)
Output

We can customize the bar chart like add labels to it, and provide labels to the axis.
1. Add Title to a Bar Plot
We pass the main
parameter to the barplot()
function to add a title to a bar chart.
numbers <- c(1, 2, 3, 4, 5)
# pass parameter main for the title
result = barplot(numbers,
main = "Numbers in Bar Chart")
barplot(result)
Output

2. Provide Labels to Axes
We use xlab
and ylab
to provide labels for the x-axis and y-axis in the barplot.
temperatures <- c(22, 27, 26, 24, 23, 26, 28)
result <- barplot(temperatures,
main = "Maximum Temperatures in a Week",
xlab = "Degree Celsius",
ylab = "Day")
print(result)
Output

3. Provide Names for Each Bar Plot
We use names.arg
to provide names for each bar plot.
temperatures <- c(22, 27, 26, 24, 23, 26, 28)
result <- barplot(temperatures,
main = "Maximum Temperatures in a Week",
xlab = "Degree Celsius",
ylab = "Day",
names.arg = c("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat")
)
print(result)
Output

R Histogram
Histogram is a graphical representation of discrete and continuous data measured on an interval scale.
We can create a histogram using hist()
function. For example,
# create a vector of data
data <- c(1, 2, 3, 4, 5)
# create a histogram
hist(data)
Output

Here are the different operations we can perform on histograms.
1. Add Title and Label to a Histogram
We use the main
parameter to a hist()
function to add a title to a histogram and xlab
and ylab
to add labels to a histogram. For example,
temperatures <- c(67 ,72 ,74 ,62 ,76 ,66 ,65 ,59 ,61 ,69 )
# histogram of temperatures vector
result <- hist(temperatures,
main = "Histogram of Temperature",
xlab = "Temperature in degrees Fahrenheit"
)
print(result)
Output

2. Change Bar Color of Histogram
To change the bar color, we pass the col
parameter inside the hist()
function.
temperatures <- c(67 ,72 ,74 ,62 ,76 ,66 ,65 ,59 ,61 ,69 )
# histogram of temperatures vector
result <- hist(temperatures,
main = "Histogram of Temperature",
xlab = "Temperature in degrees Fahrenheit",
col = "beige")
print(result)
Output

R Pie Chart
Pie chart is a circular statistical graphic divided into slices to illustrate numerical proportions.
We use the pie()
function to create a pie chart. For example,
expenditure <- c(100, 650, 150, 300, 200)
# pie chart of expenditure vector
pie(expenditure)
Output

This pie chart is plain and simple, we can add so many things to it.
1. Add Title to a Pie Chart
We pass the main
parameter inside the pie()
function to add a title to a pie chart.
expenditure <- c(100, 650, 150, 300, 200)
result <- pie(expenditure,
main = "Monthly Expenditure Breakdown"
)
print(result)
Output

2. Add Labels to Each Pie Chart Slice
We pass the labels
parameter inside the pie()
function to provide labels to each slice of a pie chart.
expenditure <- c(100, 650, 150, 300, 200)
result <- pie(expenditure,
main = "Monthly Expenditure Breakdown",
labels = c("Groceries", "Housing", "Books", "Entertainment", "Other")
)
print(result)
Output

3. Change Color of Pie Slices
We use the col
parameter inside pie()
to change the color of pie slices.
expenditure <- c(100, 650, 150, 300, 200)
result <- pie(expenditure,
main = "Monthly Expenditure Breakdown",
labels = c("Groceries", "Housing", "Books", "Entertainment", "Other"),
col = c("violet", "lavender", "beige", "gray", "pink")
)
print(result)
Output

4. Create a 3D Pie Chart
To create a 3D pie chart, we need to first import the plotrix
package. Then, we use the pie3D()
function.
# import plotrix to use pie3D()
library(plotrix)
expenditure <- c(100, 650, 150, 300, 200)
# use plot3D() to plot 3D pie chart
result <- pie3D(expenditure,
main = "Monthly Expenditure Breakdown",
labels = c("Groceries", "Housing", "Books", "Entertainment", "Other"),
col = c("violet", "lavender", "beige", "gray", "pink")
)
print(result)
Output

R Strip Chart
Strip Chart is a chart that displays a continuous stream of data over time. To plot a strip chart, we need a large set of data.
To illustrate an example of a a strip chart, we will use the built-in dataset called airquality
.
Before we plot data from airquality
, let's see the first six rows of the dataset using the head()
function.
head(airquality)
Output
Ozone Solar.R Wind Temp Month Day 1 41 190 7.4 67 5 1 2 36 118 8.0 72 5 2 3 12 149 12.6 74 5 3 4 18 313 11.5 62 5 4 5 NA NA 14.3 56 5 5 6 28 NA 14.9 66 5 6
As we can see, the airquality set has columns like Ozone, Solar.R, Wind, Temp, Month, and Day.
Now we will perform different operations to this dataset in strip chart.
1. Create Strip Chart
We use stripchart()
to create a strip chart.
# plot data of Ozone reading from airquality dataset
stripchart(airquality$Ozone)
Output

2. Add Title, Label, and Color to a Strip Chart
We pass parameters to stripchart()
function such as
main
: to add a title to a strip chartxlab
: to add a label horizontal axisylab
: to add a label in vertical axiscol
: to change the color of chart
Let's see an example.
stripchart(airquality$Ozone,
main="Mean ozone in parts per billion at Roosevelt Island",
xlab="Parts Per Billion",
ylab="Ozone",
col="blue")
Output

R BoxPlot
Boxplot is a data visualization method that indicates how the values in the data are spread out.
To plot a boxplot, we need a large set of data.
To illustrate an example of a boxplot, we will use the built-in dataset airquality
.
1. Create boxplot
We use boxplot()
functin to create a boxplot.
# plot data of Ozone reading from airquality dataset
boxplot(airquality$Ozone)
Output

2. Add Title, Label, and Color to a BoxPlot
boxplot(airquality$Ozone,
main="Mean ozone in parts per billion at Roosevelt Island",
xlab="Parts Per Billion",
ylab="Ozone",
col="blue"
)
Output

R Save Plot
We can save all the graphs like bar plots, pie chart, histogram, strip chart, and boxplot.
To illustrate the example, we will use the wind
column of the built-in dataset airquality
.
We will create and save a histogram plot to demonstrate how we can save plots.
1. Save Plot as jpeg image
We use the jpeg()
function to save the plot in jpeg format.
# save histogram in jpeg format in current directory
jpeg(file="histogram1.jpeg")
# a histogram we want to save
hist(airquality$Wind)
# call this function to save the file
dev.off()
We can also specify the full path of the file we want to save if we don't want to save it in the current directory as: jpeg(file="C:/Programiz/R-tutorial/histogram1.jpeg").
2. Save Plot as png image
We use png()
to save plot in png format.
# save as png image in specific directory with 600*350 resolution
png(file="C:/Programiz/R-tutorial/histrogram2.png",
width=600, height=350)
# a histogram we want to save
hist(airquality$Wind)
# a function call to save the file
dev.off()
3. Save Plot as pdf Image
We use pdf()
fucntion to save plot in pdf format.
# save histogram in pdf format in current directory
pdf(file="histogram1.pdf")
# a histogram we want to save
hist(airquality$Wind)
# call this function to save the file
dev.off()
File Input/Output
We can read and write CSV and xlsx files in R.
CSV (Comma Separated Value) file is a plain text file that uses comma to seperate values. Here is a sample CSV file named person.csv
.
Name, Age, Gender
John Smith, 35, Male
Jane Doe, 28, Female
Bob Johnson, 42, Male
Olivia Jolie, 29, Female
R provides different built-in functions to read and write CSV files.
1. Read a CSV File
We use the read.csv()
function to read a CSV file available in our current directory.
# read person.csv file from our current directory
read_data <- read.csv("person.csv")
# display csv file
print(read_data)
Output
Name, Age, Gender 1 John Smith 35 Male 2 Jane Doe 28 Female 3 Bob Johnson 42 Male 4 Olivia Jolie 29 Female
If the file person.csv
is in some other location, we have to specify the path along with the file names as: read.csv("D:/folder1/person.csv")
.
2. Find the total number of rows and columns
We use ncol()
and nrow()
to get the total number of rows and columns present in the CSV file.
read_data <- read.csv("person.csv")
# print total number of columns
cat("Total Columns:", ncol(read_data))
# print total number of rows
cat("Total rows:", nrow(read_data))
Output
Total Columns: 3 Total Rows: 4
3. Filter the data of CSV file
We can use the subset()
function to filter data from a CSV file that satisfies the specified condition.
read_data <- read.csv("person.csv")
# return subset of csv where
# age is greater than 30
result <- subset(read_data, Age > 30)
print(result)
Output
Name, Age, Gender 1 John Smith 35 Male 3 Bob Johnson 42 Male
4. Write Into CSV File
We use the write.csv()
function to write a data frame or a matrix to a CSV file.
# create a data frame
voters <- data.frame (
Name = c("Juan", "John", "Peter"),
Age = c(25, 15, 19),
Vote = c(TRUE, FALSE, TRUE)
)
# write dataframe voters into file1 csv file
write.csv(voters, "file1.csv")
Here, we have written data into the file named file1.csv
.
Subscribe to Programiz PRO Blog!
Be the first to receive the latest tutorial from Programiz by signing up to our email subscription. Also more bonus like inside looks on the latest feature and many more.