0% found this document useful (0 votes)
3 views17 pages

R Tutorial

R is a programming language widely used for statistical computing and data visualization, offering various statistical techniques and a supportive community. It allows for easy variable declaration without type restrictions and includes basic data types like numeric, character, and logical. The document also covers control structures like for and while loops, as well as plotting functions for creating various types of graphs such as scatter plots, pie charts, and bar charts.

Uploaded by

anshgupt4525
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views17 pages

R Tutorial

R is a programming language widely used for statistical computing and data visualization, offering various statistical techniques and a supportive community. It allows for easy variable declaration without type restrictions and includes basic data types like numeric, character, and logical. The document also covers control structures like for and while loops, as well as plotting functions for creating various types of graphs such as scatter plots, pie charts, and bar charts.

Uploaded by

anshgupt4525
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

R- tutorial​

R is a popular programming language used for statistical computing and graphical


presentation.

Its most common use is to analyze and visualize data.

Why Use R?
●​ It is a great resource for data analysis, data visualization, data science and
machine learning
●​ It provides many statistical techniques (such as statistical tests, classification,
clustering and data reduction)
●​ It is easy to draw graphs in R, like pie charts, histograms, box plot, scatter plot,
etc++
●​ It works on different platforms (Windows, Mac, Linux)
●​ It is open-source and free
●​ It has a large community support
●​ It has many packages (libraries of functions) that can be used to solve different
problems

(# This is a comment

"Hello World!")-------------single line comment

(# This is a comment

# written in

# more than just one line

"Hello World!")--------------multi line comment


Variable:-​
name <- "John"

age <- 40

name # output "John"

age # output 40​




datatype:In R, variables do not need to be declared with any particular type, and can
even change type after they have been set:​

my_var <- 30 # my_var is type of numeric

my_var <- "Sally" # my_var is now of type character (aka string)

Basic data types in R can be divided into the following types:

●​ numeric - (10.5, 55, 787)


●​ integer - (1L, 55L, 100L, where the letter "L" declares this as an integer)
●​ complex - (9 + 3i, where "i" is the imaginary part)
●​ character (a.k.a. string) - ("k", "R is exciting", "FALSE", "11.5")
●​ logical (a.k.a. boolean) - (TRUE or FALSE)

For Loops
A for loop is used for iterating over a sequence:

for (x in 1:10) {

print(x)}
Example 2:

fruits <- list("apple", "banana", "cherry")

for (x in fruits) {

print(x)

}​

R While Loops

With the while loop we can execute a set of statements as long as a condition is
TRUE:

i <- 1

while (i < 6) {

print(i)

i <- i + 1

}
R Plotting​
Plot
The plot() function is used to draw points (markers) in a diagram.

The function takes parameters for specifying points in the diagram.

Parameter 1 specifies points on the x-axis.

Parameter 2 specifies points on the y-axis.

At its simplest, you can use the plot() function to plot two numbers against each
other:​

Example:
x <- c(1, 2, 3, 4, 5)
y <- c(3, 7, 8, 9, 12)
plot(x, y)​

Draw a Line
The plot() function also takes a type parameter with the value l to draw a line to
connect all the points in the diagram:
Plot Labels
The plot() function also accept other parameters, such as main, xlab and ylab if
you want to customize the graph with a main title and different labels for the x and
y-axis:
Point Shape
Use pch with a value from 0 to 25 to change the point shape format:
Line Graphs
A line graph has a line that connects all the points in a diagram.

To create a line, use the plot() function and add the type parameter with a value of
"l":

Multiple Lines
To display more than one line in a graph, use the plot() function together with the
lines() function:
Scatter Plots
You learned from the Plot chapter that the plot() function is used to plot numbers
against each other.

A "scatter plot" is a type of plot used to display the relationship between two numerical
variables, and plots one dot for each observation.

It needs two vectors of same length, one for the x-axis (horizontal) and one for the
y-axis (vertical).​



Pie Charts
A pie chart is a circular graphical view of data.

Use the pie() function to draw pie charts.​


Labels and Header


Use the label parameter to add a label to the pie chart, and use the main parameter
to add a header:​

Bar Charts
A bar chart uses rectangular bars to visualize data. Bar charts can be displayed
horizontally or vertically. The height or length of the bars are proportional to the values
they represent.

Use the barplot() function to draw a vertical bar chart:



Density / Bar Texture
To change the bar texture, use the density parameter:

Example

x <- c("A", "B", "C", "D")

y <- c(2, 4, 6, 8)

barplot(y, [Link] = x, density = 10)

Result:

You might also like