R programming

Pramodkumar Jha
Pramodkumar JhaBusiness Analyst um Onsite Electro Services Pvt. Ltd.
Let’s Start
Basic Computation
# Addition:
> 2 + 3
>5
# Subtraction:
> 10 - 5
>5
# Division:
>6/3
>5
# Multiplication:
>2.5*2
>5
# Log:
>log(12)
>1.07
# Square root:
>sqrt(121)
>11
# Addition:
>a <- 2 + 3
>a
>5
# Subtraction:
>b <- 10 - 5
>b
>5
# Division:
>c <- 6/3
>c
>5
# Multiplication:
>d <- 2.5*2
>d
>d
# Log:
>e <- log(12)
>e
>1.07
# Square root:
>f <- sqrt(121)
>f
>11
# Calculation
# Assigning Variable
# To assign variable use ‘<-’ sign
# Note: Use ‘Ctrl + L’ to clear R console
# lists current objects, let’s check the all the object created in R
using below syntax
ls()
# Removes specified objects
rm(“a”)
# Removes all objects
rm(list=ls())
# Import inbuilt data set in R
data(mtcars)
# View object in new screen
View(mtcars)
# Starts empty GUI spreadsheet editor for manual data entry.
x = edit(data.frame())
# Returns an object's attribute list.
attributes(mtcars)
# Returns the dimensions of vectors, arrays or dataframes
dim(mtcars)
# Lists content of current working directory
dir()
Some Key….KEY
R has five basic or ‘atomic’ classes of objects. Wait, what is an object ?
• Everything you see or create in R is an object.
• A vector, matrix, data frame, even a variable is an object.
• R has 5 basic classes of objects. This includes:
1. Character
2. Numeric (Real Numbers)
3. Integer (Whole Numbers)
4. Complex
5. Logical (True / False)
Think of attributes as their ‘identifier’, a name or number which identifies them. An object can have following attributes:
1.names, dimension names
2.dimensions
3.class
4.Length
• Attributes of an object can be accessed using attributes() function.
• The most basic object in R is known as vector.
• You can create an empty vector using vector().
Remember, a vector contains object of same class.
For example: Let’s create vectors of different classes. We can create vector using c() or concatenate command also.
> a <- c(1.8, 4.5) #numeric
> b <- c(1 + 2i, 3 - 6i) #complex
> c <- c(23, 44) #integer
> d <- c(T,F,TRUE,FALSE) #logical
Essential of R Programming
List:
A list is a special type of vector which contain elements of different data types.
For example:
> my_list <- list(22, "ab", TRUE, 1 + 2i)
> my_list
[[1]]
[1] 22
[[2]]
[1] "ab"
[[3]]
[1] TRUE
[[4]]
[1] 1+2i
As you can see, the output of a list is different from a vector.
This is because, all the objects are of different types.
The double bracket [[1]] shows the index of first element and so on. Hence, you can easily extract the element of lists
depending on their index. Like this:
> my_list[[3]]
> [1] TRUE
You can use [] single bracket too. But, that would return the list element with its index number, instead of the result above.
Like this:
> my_list[3]
> [[1]]
[1] TRUE
Data Type - R
.
Vector:
• Contains object of same class.
• you can mix objects of different classes too.
• coercion occurs, when different classes are mixed
• coercion means ‘convert’ different class into one class.
For example:
> qt <- c("Time", 24, "October", TRUE, 3.33) #character
> ab <- c(TRUE, 24) #numeric
> cd <- c(2.5, "May") #character
To check the class of any object, use class(“vector
name”) function.
> class(qt)
"character"
To convert the class of a vector, you can use as. command.
>bar <- 0:5
> class(bar)
> "integer"
> as.numeric(bar)
> class(bar)
> "numeric"
> as.character(bar)
> class(bar)
> "character“
• Similarly, you can change the class of any vector.
• If you try to convert a “character” vector to “numeric” ,
NAs will be introduced.
R has various type of ‘data types’ which includes vector (numeric, integer etc), matrices, data frames and list. Let’s
understand them one by one
Data Type - R
Matrices:
When a vector is introduced with row and column i.e. a dimension attribute, it becomes a matrix. A matrix is represented by
set of rows and columns. It is a 2 dimensional data structure. It consist of elements of same class. Let’s create a matrix of 3
rows and 2 columns:
> my_matrix <- matrix(1:6, nrow=3, ncol=2)
> my_matrix
[,1] [,2]
[1,] 1 4
[2,] 2 5
[3,] 3 6
> dim(my_matrix)
[1] 3 2
> attributes(my_matrix)
$dim
[1] 3 2
As you can see, the dimensions of a matrix can be obtained using either dim() or attributes()command. To extract a
particular element from a matrix, simply use the index shown above. For example(try this at your end):
> my_matrix[,2] #extracts second column
> my_matrix[,1] #extracts first column
> my_matrix[2,] #extracts second row
> my_matrix[1,] #extracts first row
As an interesting fact, you can also create a matrix from a vector. All you need to do is, assign dimension dim() later. Like
this:
Data Type - R
Data Frame:
This is the most commonly used member of data
types family. It is used to store tabular data. It is
different from matrix. In a matrix, every element
must have same class. But, in a data frame, you can
put list of vectors containing different classes. This
means, every column of a data frame acts like a list.
Every time you will read data in R, it will be stored
in the form of a data frame. Hence, it is important
to understand the majorly used commands on data
frame:
> df <- data.frame(name =
c("ash","jane","paul","mark"), score = c(67,56,87,91))
> df
name score
1 ash 67
2 jane 56
3 paul 87
4 mark 91
> dim(df)
[1] 4 2
str(df)
'data.frame': 4 obs. of 2 variables:
$ name : Factor w/ 4 levels "ash","jane","mark",..: 1 2 4 3
$ score: num 67 56 87 91
> nrow(df)
[1] 4
> ncol(df)
[1] 2
Let’s understand the code above. df is the name of data frame.
• dim() returns the dimension of data frame as 4 rows and 2
columns.
• str() returns the structure of a data frame i.e. the list of variables
stored in the data frame.
• nrow() and ncol() return the number of rows and number of
columns in a data set respectively.
you see “name” is a factor variable and “score” is numeric.
In data science, a variable can be categorized into two types:
• Continuous and
• Categorical.
Data Type - R
Let’s Create a Vector, Metrics, Dataframe and List
• Create list of anything you want
• Create a numeric vector 1 to 5
• Create character vector a to d
• Create Metrics consist 10 rows and 10 Columns
• Create Dataframe of three column to the variable df
• First Column name ‘alpha’ and value will be from a to e
• Second column name ‘numeric’ and value will be from 1 to 5
• Third column name ‘missing’ and value will be (4,5,2,NA,NA)
Data Type – R : Exercise
Control structure ‘controls’ the flow of code / commands written inside a function.
A function is a set of multiple commands written to automate a repetitive coding task.
For example:
You have 10 data sets. You want to find the mean of ‘Age’ column present in every data set. This can be done in 2 ways: either
you write the code to compute mean 10 times or you simply create a function and pass the data set to it.
Let’s understand the control structures in R with simple examples:
if, else – This structure is used to test a condition. Below is the syntax:
if (<condition>){
##do something
} else {
##do something
}
Example
#initialize a variable
N <- 10
#check if this variable * 5 is > 40
if (N * 5 > 40){
print("This is easy!")
} else {
print ("It's not easy!")
}
[1] "This is easy!"
Control Structure
for:
This structure is used when a loop is to be
executed fixed number of times.
It is commonly used for iterating over the
elements of an object (list, vector). Below is the
syntax:
for (<search condition>){
#do something
}
Example
#initialize a vector
y <- c(99,45,34,65,76,23)
#print the first 4 numbers of this vector
for(i in 1:4){
print (y[i])
}
[1] 99
[1] 45
[1] 34
[1] 65
while:
It begins by testing a condition, and executes only if the condition is found to
be true.
Once the loop is executed, the condition is tested again. Hence, it’s necessary
to alter the condition such that the loop doesn’t go infinity. Below is the
syntax:
while (<search condition>){
#do something
}
Example
#initialize a condition
Age <- 12
#check if age is less than 17
while(Age < 17){
print(Age)
Age <- Age + 1 #Once the loop is executed, this code breaks the loop
}
[1] 12
[1] 13
[1] 14
[1] 15
[1] 16
Control Structure
Let’s Install all required Packages using Loop and If Condition
# Creating List of packages
packages <- list("XLConnect","RODBC","tm","rvest","data.table", "networkD3","webshot","ggplot2")
# Creating loop to run through list
for( package in packages){
# if condition, to check if package is already installed
if (package %in% installed.packages()[,1]){
print(paste(package,"available"))
}else{
install.packages(package, dependencies = TRUE, repos="http://cran.rstudio.com/")
}
}
Control Structure : Exercise
Let’s now understand the concept of missing values in R. This is one of the most painful yet crucial part of predictive
modeling. You must be aware of all techniques to deal with them.
Missing values in R are represented by NA and NaN. Now we’ll check if a data set has missing values (using the same data
frame df).
Create dataframe with NA:
> df = data.frame(name=c("a","b","c","d","e","f"),score=c(1,2,3,4,NA,NA))
> df
name score
1 a 1
2 b 2
3 c 3
4 d 4
5 e NA
6 f NA
> is.na(df) #checks the entire data set for NAs and return logical output
name score
name score
[1,] FALSE FALSE
[2,] FALSE FALSE
[3,] FALSE FALSE
[4,] FALSE FALSE
[5,] FALSE TRUE
[6,] FALSE TRUE
Missing Data
> table(is.na(df)) #returns a table of logical output
FALSE TRUE
10 2
> df[!complete.cases(df),] #returns the list of rows having missing values
name score
5 e NA
6 f NA
Missing values hinder normal calculations in a data set. For example, let’s say, we want to compute the mean of score. Since
there are two missing values, it can’t be done directly. Let’s see:
mean(df$score)
[1] NA
> mean(df$score, na.rm = TRUE)
[1] 2.5
The use of na.rm = TRUE parameter tells R to ignore the NAs and compute the mean of remaining values in the selected column
(score). To remove rows with NA values in a data frame, you can use na.omit:
> new_df <- na.omit(df)
> new_df
name score
1 a 1
2 b 2
3 c 3
4 d 4
Missing Data
Data frame created in Data Type session, we have ‘NA’ value in ‘missing’ column
First print data frame, type df in console
Now use:
• is.na(dataframe) # on data frame
• table(is.na(dataframe) # to know if there is any NA in data frame
• na.omit(dataframe) # to remove na from data frame
Missing Data : Exercise
# Create a folder at some location
# Set working directory to the folder
setwd(enter_path_here)
From EXCEL:
#install.packages('XLConnect', dependencies=TRUE, repos='http://cran.rstudio.com/’)
library(XLConnect)
wb = loadWorkbook(“AAPL.xlsx")
ExcelData = readWorksheet(wb, sheet = "Sheet1", header = TRUE)
From CSV:
CSVData <- read.csv(file="AAPL.csv", header=TRUE, sep=",")
From Database (SQL Database):
#install.packages("RODBC", dependencies=TRUE, repos='http://cran.rstudio.com/')
library(RODBC)
conn <- odbcDriverConnect('driver={SQL Server};server=PRANAVSQLEXPRESS;database=CentralMI;trusted_connection=true’)
SQLData <- sqlQuery(conn, "SELECT * FROM requestdetail;")
close(conn)
Read Data
From WEB:
#install.packages('rvest',dependencies = TRUE,repos = 'http://cran.rstudio.com')
library(rvest)
url <- "https://www.iplt20.com/stats/2018"
webpage <- read_html(url)
webpage1 <- webpage%>%html_nodes("table")%>%.[1]%>%html_table(fill=TRUE)
webpage2 <- data.frame(webpage1)
Inbuilt Data:
data(mtcars)
From PDF:
#install.packages('tm',dependencies=TRUE, repos='http://cran.rstudio.com/')
library('tm')
file <- 'namefile.pdf'
Rpdf <- readPDF(control = list(text = "-layout"))
corpus <- VCorpus(URISource(file), readerControl = list(reader = Rpdf))
corpus.array <- content(content(corpus)[[1]])
Read Data
Steps to Manipulate:
1) Import data
2) Sub-setting data (removing unwanted data)
3) Selecting required column
4) Selecting required row
5) Merging other data for mapping
6) Grouping / aggregate
7) Exporting data to various output
(1) Import Data
(3) Sub-setting data (removing unwanted data)
# now we required cars having 3 gear
table1 <- subset(table,table[,gear==3])
(3) Selecting required columns:
# We will select columns from 1 to 3 and 10 to 12
table2 <- table1[,c(1:3,10:12)]
(5) Merging other data for mapping
# Create mapping dataframe
mapping <- data.frame(carb=c(1,2,3,5),name=c("a","a","b“,”c”))
Inner join:
merge(x = table3, y = mapping)
Outer join:
merge(x = table3, y = mapping, by='carb', all = TRUE)
Left outer:
merge(x = table3, y = mapping, by='carb', all.x = TRUE)
Right outer:
merge(x = table3, y = mapping, by='carb', all.y = TRUE)
Cross Join:
merge(x = table3, y = mapping, by=NULL)
# Let’s create inner join and assign to variable named ‘table4’
table4 <- merge(x = table3, y = mapping)
data(mtcars)
# let’s understand data, have name.
head(mtcars)
# You will find first column do not have name, So
will import library
library(data.table)
table <- data.table(mtcars)
head(table)
(4) Selecting required rows:
# We will select columns from 1 to 3 and 10 to 12
table3<- table2[c(1:10),]
Manipulate Data
(6) Grouping/ Aggregate
# Sum of ‘mpg’ column on newly merged column ‘name’,
table5 <- aggregate(table4$mpg, by=list(Category=table4$name), FUN=sum)
# Max of ‘mpg’ column on newly merged column ‘name’,
Max <- aggregate(table4$mpg, by=list(Category=table4$name), FUN=max)
# Min of ‘mpg’ column on newly merged column ‘name’
Min <- aggregate(table4$mpg, by=list(Category=table4$name), FUN=min)
# Mean of ‘mpg’ column on newly merged column ‘name’,
Mean <- aggregate(table4$mpg, by=list(Category=table4$name), FUN=mean)
# Standard Deviation of ‘mpg’ column on newly merged column ‘name’,
Sd <- ggregate(table4$mpg, by=list(Category=table4$name), FUN=sd)
# Standard Deviation of ‘mpg’ column on newly merged column ‘name’,
Median <- aggregate(table4$mpg, by=list(Category=table4$name), FUN=median)
# Standard Deviation of ‘mpg’ column on newly merged column ‘name’,
Summary <- summary(table4)
(7) Exporting data in CSV file:
write.csv(Summary, file = "F:R Programmingtest.csv", row.names = FALSE)
Manipulate Data
To CSV:
write.csv(x, file = "F:R Programmingtest.csv", row.names = FALSE)
To Excel:
#install.packages('XLConnect', dependencies=TRUE, repos='http://cran.rstudio.com/’)
library(XLConnect)
wb = loadWorkbook(“AAPL1.xlsx")
writeWorksheetToFile(wb , data = data , sheet = "sheet1", startRow = 1, startCol = 1)
Write Data
The different parts of a function are −
Function Name − This is the actual name of the function. It is stored in R environment as an object with this name.
Arguments − An argument is a placeholder. When a function is invoked, you pass a value to the argument. Arguments are
optional; that is, a function may contain no arguments. Also arguments can have default values.
Function Body − The function body contains a collection of statements that defines what the function does.
Return Value − The return value of a function is the last expression in the function body to be evaluated.
R has many in-built functions which can be directly called in the program without defining them first. We can also create and
use our own functions referred as user defined functions.
Function
# Create a function with arguments.
myfunction <- function(a,b,c) {
result <- a * b + c
print(result)
}
# Call the function by position of arguments.
myfunction(10,11,22)
# function to take user input
readinteger <- function(){
n <- readline(prompt="Enter an integer: ")
return(as.integer(n))
}
print(readinteger()) # print input
# syntax of function
myfunction <- function(arg1, arg2, ... ){
statements
return(object)
}
function1 = function(path='F://R
Programming',filename='AAPL.xlsx’,outputfilename='AAPL1.xlsx’,sheetname=‘Sheet1',skiprows=0,groupby=none,aggregate=sum)
{
# joining path and file name
filedetail = paste(path, "/",filename,sep="")
outputdetail = paste(path, "/", outputfilename,sep="")
# if condition to check, package is already installed
if('XLConnect' %in% installed.packages()[,1]) { }else{install.packages('XLConnect', dependencies=TRUE, repos='http://cran.rstudio.com/’)}
# importing library
library(XLConnect)
# Loading workbook
wb = loadWorkbook(filedetail)
# Reading data from workbook
df = readWorksheet(wb , sheet = sheetname, header = TRUE)
# select few top records for display
head(df)
# Summary of dataset
summary = summary(df)
# write to Excel file
writeWorksheetToFile(outputdetail , data = summary , sheet = "sheet1", startRow = 3, startCol = 4)
}
#Calling function
function1()
Function – Build Own Function
setwd()
# Let’s create chart with built in data set
# Step 1) Import data
data(mtcars)
# load library
library(data.table)
# use data.table library to get name of first column
table = data.table(mtcars, keep.rownames=TRUE)
# select 1st and 2nd column for charts
table1 = table[,1:2]
# Give the chart file a name
png(file = "barchart_Cars_mpg.png")
# data is ready for charts
h <- table1$mpg
y <- table1$rn
x <- table1$mpg
barplot(h,xlab="Cars",ylab="mpg",names.arg=y,,col="red",main="
Cars-mpg",border="red")
dev.off()
#syntax of barplot
barplot(H,xlab,ylab,main, names.arg,col)
• H is a vector or matrix containing numeric values used
in bar chart.
• xlab is the label for x axis.
• ylab is the label for y axis.
• main is the title of the bar chart.
• names.arg is a vector of names appearing under each
bar.
• col is used to give colors to the bars in the graph.
Charts
if('networkD3' %in% installed.packages()[,1]) { }else{install.packages('networkD3', dependencies=TRUE, repos='http://cran.rstudio.com/')}
if('webshot' %in% installed.packages()[,1]) { }else{install.packages('webshot', dependencies=TRUE, repos='http://cran.rstudio.com/')}
# Load package
library(networkD3)
library(webshot)
# create data:
set.seed(101)
links=data.frame(
source=c("A","A", "A", "A", "A","J", "B", "B", "C", "C", "D","I"),
target=c("B","B", "C", "D", "J","A","E", "F", "G", "H", "I","I")
)
# Plot
graph = simpleNetwork(links,
Source = 1, # column number of source
Target = 2, # column number of target
height = 480, # height of frame area in pixels
width = 480,
linkDistance = 120, # distance between node. Increase this value to have more space between nodes
charge = -480, # numeric value indicating either the strength of the node repulsion (negative value) or attraction (positive value)
fontSize = 22, # size of the node names
fontFamily = "serif", # font og node names
linkColour = "#666", # colour of edges, MUST be a common colour for the whole graph
nodeColour = "red", # colour of nodes, MUST be a common colour for the whole graph
opacity = 0.8, # opacity of nodes. 0=transparent. 1=no transparency
zoom = T # Can you zoom on the figure?)
saveNetwork(graph,file = '#252_interactive_network_chart1.html',selfcontained = T)
Network chart with D3 using R – Sample 1
# libraries
library(networkD3)
# Load data
data(MisLinks)
data(MisNodes)
# Plot
forceNetwork(
Links = MisLinks, Nodes = MisNodes,
Source = "source", Target = "target",
Value = "value", NodeID = "name",
Group = "group", opacity = 0.8,
linkDistance = JS('function(){d3.select("body").style("background-color", "#DAE3F9"); return 50;}')
)
Network chart with D3 using R – Sample 2
Inbuilt Packaged to Learn
1 von 26

Recomendados

Basic R Data Manipulation von
Basic R Data ManipulationBasic R Data Manipulation
Basic R Data ManipulationChu An
117 views51 Folien
[1062BPY12001] Data analysis with R / week 2 von
[1062BPY12001] Data analysis with R / week 2[1062BPY12001] Data analysis with R / week 2
[1062BPY12001] Data analysis with R / week 2Kevin Chun-Hsien Hsu
158 views33 Folien
R Basics von
R BasicsR Basics
R BasicsDr.E.N.Sathishkumar
1.1K views67 Folien
R Programming: Learn To Manipulate Strings In R von
R Programming: Learn To Manipulate Strings In RR Programming: Learn To Manipulate Strings In R
R Programming: Learn To Manipulate Strings In RRsquared Academy
2.5K views41 Folien
Python lecture 05 von
Python lecture 05Python lecture 05
Python lecture 05Tanwir Zaman
415 views55 Folien
Introduction to R von
Introduction to RIntroduction to R
Introduction to Ragnonchik
282 views37 Folien

Más contenido relacionado

Was ist angesagt?

Ggplot2 v3 von
Ggplot2 v3Ggplot2 v3
Ggplot2 v3Josh Doyle
1.2K views37 Folien
R Programming: Transform/Reshape Data In R von
R Programming: Transform/Reshape Data In RR Programming: Transform/Reshape Data In R
R Programming: Transform/Reshape Data In RRsquared Academy
1.6K views60 Folien
Python programming -Tuple and Set Data type von
Python programming -Tuple and Set Data typePython programming -Tuple and Set Data type
Python programming -Tuple and Set Data typeMegha V
175 views24 Folien
Data handling in r von
Data handling in rData handling in r
Data handling in rAbhik Seal
1.7K views40 Folien
A Tour to MySQL Commands von
A Tour to MySQL CommandsA Tour to MySQL Commands
A Tour to MySQL CommandsHikmat Dhamee
435 views15 Folien
Data manipulation on r von
Data manipulation on rData manipulation on r
Data manipulation on rAbhik Seal
1.2K views32 Folien

Was ist angesagt?(20)

R Programming: Transform/Reshape Data In R von Rsquared Academy
R Programming: Transform/Reshape Data In RR Programming: Transform/Reshape Data In R
R Programming: Transform/Reshape Data In R
Rsquared Academy1.6K views
Python programming -Tuple and Set Data type von Megha V
Python programming -Tuple and Set Data typePython programming -Tuple and Set Data type
Python programming -Tuple and Set Data type
Megha V175 views
Data handling in r von Abhik Seal
Data handling in rData handling in r
Data handling in r
Abhik Seal1.7K views
Data manipulation on r von Abhik Seal
Data manipulation on rData manipulation on r
Data manipulation on r
Abhik Seal1.2K views
Introduction sql von sagarasuri
Introduction sqlIntroduction sql
Introduction sql
sagarasuri489 views
Basic operations by novi reandy sasmita von beasiswa
Basic operations by novi reandy sasmitaBasic operations by novi reandy sasmita
Basic operations by novi reandy sasmita
beasiswa358 views
Cheat sheet python3 von sxw2k
Cheat sheet python3Cheat sheet python3
Cheat sheet python3
sxw2k306 views
Stata Programming Cheat Sheet von Laura Hughes
Stata Programming Cheat SheetStata Programming Cheat Sheet
Stata Programming Cheat Sheet
Laura Hughes1.4K views
The Ring programming language version 1.7 book - Part 35 of 196 von Mahmoud Samir Fayed
The Ring programming language version 1.7 book - Part 35 of 196The Ring programming language version 1.7 book - Part 35 of 196
The Ring programming language version 1.7 book - Part 35 of 196
Very basic functional design patterns von Tomasz Kowal
Very basic functional design patternsVery basic functional design patterns
Very basic functional design patterns
Tomasz Kowal820 views

Similar a R programming

R Programming.pptx von
R Programming.pptxR Programming.pptx
R Programming.pptxkalai75
9 views27 Folien
Day 1d R structures & objects: matrices and data frames.pptx von
Day 1d   R structures & objects: matrices and data frames.pptxDay 1d   R structures & objects: matrices and data frames.pptx
Day 1d R structures & objects: matrices and data frames.pptxAdrien Melquiond
133 views17 Folien
R tutorial for a windows environment von
R tutorial for a windows environmentR tutorial for a windows environment
R tutorial for a windows environmentYogendra Chaubey
876 views74 Folien
A quick introduction to R von
A quick introduction to RA quick introduction to R
A quick introduction to RAngshuman Saha
1.3K views45 Folien
Programming in R von
Programming in RProgramming in R
Programming in RSmruti Sarangi
826 views53 Folien
Python Day1 von
Python Day1Python Day1
Python Day1Mantavya Gajjar
1.8K views34 Folien

Similar a R programming(20)

R Programming.pptx von kalai75
R Programming.pptxR Programming.pptx
R Programming.pptx
kalai759 views
Day 1d R structures & objects: matrices and data frames.pptx von Adrien Melquiond
Day 1d   R structures & objects: matrices and data frames.pptxDay 1d   R structures & objects: matrices and data frames.pptx
Day 1d R structures & objects: matrices and data frames.pptx
Adrien Melquiond133 views
Big Data Mining in Indian Economic Survey 2017 von Parth Khare
Big Data Mining in Indian Economic Survey 2017Big Data Mining in Indian Economic Survey 2017
Big Data Mining in Indian Economic Survey 2017
Parth Khare257 views
Day 1c access, select ordering copy.pptx von Adrien Melquiond
Day 1c   access, select   ordering copy.pptxDay 1c   access, select   ordering copy.pptx
Day 1c access, select ordering copy.pptx
Adrien Melquiond580 views
Introduction to R r.nabati - iausdj.ac.ir von nabati
Introduction to R   r.nabati - iausdj.ac.irIntroduction to R   r.nabati - iausdj.ac.ir
Introduction to R r.nabati - iausdj.ac.ir
nabati90 views
Array 31.8.2020 updated von vrgokila
Array 31.8.2020 updatedArray 31.8.2020 updated
Array 31.8.2020 updated
vrgokila86 views
3. R- list and data frame von krishna singh
3. R- list and data frame3. R- list and data frame
3. R- list and data frame
krishna singh2.3K views
An overview of Python 2.7 von decoupled
An overview of Python 2.7An overview of Python 2.7
An overview of Python 2.7
decoupled248 views
Pythonlearn-08-Lists.pptx von MihirDatir
Pythonlearn-08-Lists.pptxPythonlearn-08-Lists.pptx
Pythonlearn-08-Lists.pptx
MihirDatir10 views

Más de Pramodkumar Jha

Macros for Border, Weight Clear Data von
Macros for Border, Weight Clear DataMacros for Border, Weight Clear Data
Macros for Border, Weight Clear DataPramodkumar Jha
90 views10 Folien
Macros code Copy paste visible data after filtering von
Macros code Copy paste visible data after filteringMacros code Copy paste visible data after filtering
Macros code Copy paste visible data after filteringPramodkumar Jha
72 views10 Folien
Macros code For Loop von
Macros code For LoopMacros code For Loop
Macros code For LoopPramodkumar Jha
85 views10 Folien
Macros code for Font, Style, Size, Bold von
Macros code for Font, Style, Size, BoldMacros code for Font, Style, Size, Bold
Macros code for Font, Style, Size, BoldPramodkumar Jha
106 views10 Folien
Macros code for Protecting and Unprotecting Sheets von
Macros code for Protecting and Unprotecting SheetsMacros code for Protecting and Unprotecting Sheets
Macros code for Protecting and Unprotecting SheetsPramodkumar Jha
72 views10 Folien
Macros for Shape and formula von
Macros for Shape and formulaMacros for Shape and formula
Macros for Shape and formulaPramodkumar Jha
74 views15 Folien

Más de Pramodkumar Jha(20)

Macros code Copy paste visible data after filtering von Pramodkumar Jha
Macros code Copy paste visible data after filteringMacros code Copy paste visible data after filtering
Macros code Copy paste visible data after filtering
Pramodkumar Jha72 views
Macros code for Font, Style, Size, Bold von Pramodkumar Jha
Macros code for Font, Style, Size, BoldMacros code for Font, Style, Size, Bold
Macros code for Font, Style, Size, Bold
Pramodkumar Jha106 views
Macros code for Protecting and Unprotecting Sheets von Pramodkumar Jha
Macros code for Protecting and Unprotecting SheetsMacros code for Protecting and Unprotecting Sheets
Macros code for Protecting and Unprotecting Sheets
Pramodkumar Jha72 views

Último

Advanced_Recommendation_Systems_Presentation.pptx von
Advanced_Recommendation_Systems_Presentation.pptxAdvanced_Recommendation_Systems_Presentation.pptx
Advanced_Recommendation_Systems_Presentation.pptxneeharikasingh29
5 views9 Folien
MOSORE_BRESCIA von
MOSORE_BRESCIAMOSORE_BRESCIA
MOSORE_BRESCIAFederico Karagulian
5 views8 Folien
Short Story Assignment by Kelly Nguyen von
Short Story Assignment by Kelly NguyenShort Story Assignment by Kelly Nguyen
Short Story Assignment by Kelly Nguyenkellynguyen01
19 views17 Folien
Survey on Factuality in LLM's.pptx von
Survey on Factuality in LLM's.pptxSurvey on Factuality in LLM's.pptx
Survey on Factuality in LLM's.pptxNeethaSherra1
6 views9 Folien
VoxelNet von
VoxelNetVoxelNet
VoxelNettaeseon ryu
7 views21 Folien
Cross-network in Google Analytics 4.pdf von
Cross-network in Google Analytics 4.pdfCross-network in Google Analytics 4.pdf
Cross-network in Google Analytics 4.pdfGA4 Tutorials
6 views7 Folien

Último(20)

Advanced_Recommendation_Systems_Presentation.pptx von neeharikasingh29
Advanced_Recommendation_Systems_Presentation.pptxAdvanced_Recommendation_Systems_Presentation.pptx
Advanced_Recommendation_Systems_Presentation.pptx
Short Story Assignment by Kelly Nguyen von kellynguyen01
Short Story Assignment by Kelly NguyenShort Story Assignment by Kelly Nguyen
Short Story Assignment by Kelly Nguyen
kellynguyen0119 views
Survey on Factuality in LLM's.pptx von NeethaSherra1
Survey on Factuality in LLM's.pptxSurvey on Factuality in LLM's.pptx
Survey on Factuality in LLM's.pptx
NeethaSherra16 views
Cross-network in Google Analytics 4.pdf von GA4 Tutorials
Cross-network in Google Analytics 4.pdfCross-network in Google Analytics 4.pdf
Cross-network in Google Analytics 4.pdf
GA4 Tutorials6 views
Data about the sector workshop von info828217
Data about the sector workshopData about the sector workshop
Data about the sector workshop
info82821712 views
CRIJ4385_Death Penalty_F23.pptx von yvettemm100
CRIJ4385_Death Penalty_F23.pptxCRIJ4385_Death Penalty_F23.pptx
CRIJ4385_Death Penalty_F23.pptx
yvettemm1006 views
OECD-Persol Holdings Workshop on Advancing Employee Well-being in Business an... von StatsCommunications
OECD-Persol Holdings Workshop on Advancing Employee Well-being in Business an...OECD-Persol Holdings Workshop on Advancing Employee Well-being in Business an...
OECD-Persol Holdings Workshop on Advancing Employee Well-being in Business an...
[DSC Europe 23] Spela Poklukar & Tea Brasanac - Retrieval Augmented Generation von DataScienceConferenc1
[DSC Europe 23] Spela Poklukar & Tea Brasanac - Retrieval Augmented Generation[DSC Europe 23] Spela Poklukar & Tea Brasanac - Retrieval Augmented Generation
[DSC Europe 23] Spela Poklukar & Tea Brasanac - Retrieval Augmented Generation
CRM stick or twist workshop von info828217
CRM stick or twist workshopCRM stick or twist workshop
CRM stick or twist workshop
info8282179 views
[DSC Europe 23][Cryptica] Martin_Summer_Digital_central_bank_money_Ideas_init... von DataScienceConferenc1
[DSC Europe 23][Cryptica] Martin_Summer_Digital_central_bank_money_Ideas_init...[DSC Europe 23][Cryptica] Martin_Summer_Digital_central_bank_money_Ideas_init...
[DSC Europe 23][Cryptica] Martin_Summer_Digital_central_bank_money_Ideas_init...
SUPER STORE SQL PROJECT.pptx von khan888620
SUPER STORE SQL PROJECT.pptxSUPER STORE SQL PROJECT.pptx
SUPER STORE SQL PROJECT.pptx
khan88862012 views
[DSC Europe 23] Zsolt Feleki - Machine Translation should we trust it.pptx von DataScienceConferenc1
[DSC Europe 23] Zsolt Feleki - Machine Translation should we trust it.pptx[DSC Europe 23] Zsolt Feleki - Machine Translation should we trust it.pptx
[DSC Europe 23] Zsolt Feleki - Machine Translation should we trust it.pptx
CRM stick or twist.pptx von info828217
CRM stick or twist.pptxCRM stick or twist.pptx
CRM stick or twist.pptx
info82821710 views
UNEP FI CRS Climate Risk Results.pptx von pekka28
UNEP FI CRS Climate Risk Results.pptxUNEP FI CRS Climate Risk Results.pptx
UNEP FI CRS Climate Risk Results.pptx
pekka2811 views
Ukraine Infographic_22NOV2023_v2.pdf von AnastosiyaGurin
Ukraine Infographic_22NOV2023_v2.pdfUkraine Infographic_22NOV2023_v2.pdf
Ukraine Infographic_22NOV2023_v2.pdf
AnastosiyaGurin1.4K views

R programming

  • 2. Basic Computation # Addition: > 2 + 3 >5 # Subtraction: > 10 - 5 >5 # Division: >6/3 >5 # Multiplication: >2.5*2 >5 # Log: >log(12) >1.07 # Square root: >sqrt(121) >11 # Addition: >a <- 2 + 3 >a >5 # Subtraction: >b <- 10 - 5 >b >5 # Division: >c <- 6/3 >c >5 # Multiplication: >d <- 2.5*2 >d >d # Log: >e <- log(12) >e >1.07 # Square root: >f <- sqrt(121) >f >11 # Calculation # Assigning Variable # To assign variable use ‘<-’ sign # Note: Use ‘Ctrl + L’ to clear R console
  • 3. # lists current objects, let’s check the all the object created in R using below syntax ls() # Removes specified objects rm(“a”) # Removes all objects rm(list=ls()) # Import inbuilt data set in R data(mtcars) # View object in new screen View(mtcars) # Starts empty GUI spreadsheet editor for manual data entry. x = edit(data.frame()) # Returns an object's attribute list. attributes(mtcars) # Returns the dimensions of vectors, arrays or dataframes dim(mtcars) # Lists content of current working directory dir() Some Key….KEY
  • 4. R has five basic or ‘atomic’ classes of objects. Wait, what is an object ? • Everything you see or create in R is an object. • A vector, matrix, data frame, even a variable is an object. • R has 5 basic classes of objects. This includes: 1. Character 2. Numeric (Real Numbers) 3. Integer (Whole Numbers) 4. Complex 5. Logical (True / False) Think of attributes as their ‘identifier’, a name or number which identifies them. An object can have following attributes: 1.names, dimension names 2.dimensions 3.class 4.Length • Attributes of an object can be accessed using attributes() function. • The most basic object in R is known as vector. • You can create an empty vector using vector(). Remember, a vector contains object of same class. For example: Let’s create vectors of different classes. We can create vector using c() or concatenate command also. > a <- c(1.8, 4.5) #numeric > b <- c(1 + 2i, 3 - 6i) #complex > c <- c(23, 44) #integer > d <- c(T,F,TRUE,FALSE) #logical Essential of R Programming
  • 5. List: A list is a special type of vector which contain elements of different data types. For example: > my_list <- list(22, "ab", TRUE, 1 + 2i) > my_list [[1]] [1] 22 [[2]] [1] "ab" [[3]] [1] TRUE [[4]] [1] 1+2i As you can see, the output of a list is different from a vector. This is because, all the objects are of different types. The double bracket [[1]] shows the index of first element and so on. Hence, you can easily extract the element of lists depending on their index. Like this: > my_list[[3]] > [1] TRUE You can use [] single bracket too. But, that would return the list element with its index number, instead of the result above. Like this: > my_list[3] > [[1]] [1] TRUE Data Type - R
  • 6. . Vector: • Contains object of same class. • you can mix objects of different classes too. • coercion occurs, when different classes are mixed • coercion means ‘convert’ different class into one class. For example: > qt <- c("Time", 24, "October", TRUE, 3.33) #character > ab <- c(TRUE, 24) #numeric > cd <- c(2.5, "May") #character To check the class of any object, use class(“vector name”) function. > class(qt) "character" To convert the class of a vector, you can use as. command. >bar <- 0:5 > class(bar) > "integer" > as.numeric(bar) > class(bar) > "numeric" > as.character(bar) > class(bar) > "character“ • Similarly, you can change the class of any vector. • If you try to convert a “character” vector to “numeric” , NAs will be introduced. R has various type of ‘data types’ which includes vector (numeric, integer etc), matrices, data frames and list. Let’s understand them one by one Data Type - R
  • 7. Matrices: When a vector is introduced with row and column i.e. a dimension attribute, it becomes a matrix. A matrix is represented by set of rows and columns. It is a 2 dimensional data structure. It consist of elements of same class. Let’s create a matrix of 3 rows and 2 columns: > my_matrix <- matrix(1:6, nrow=3, ncol=2) > my_matrix [,1] [,2] [1,] 1 4 [2,] 2 5 [3,] 3 6 > dim(my_matrix) [1] 3 2 > attributes(my_matrix) $dim [1] 3 2 As you can see, the dimensions of a matrix can be obtained using either dim() or attributes()command. To extract a particular element from a matrix, simply use the index shown above. For example(try this at your end): > my_matrix[,2] #extracts second column > my_matrix[,1] #extracts first column > my_matrix[2,] #extracts second row > my_matrix[1,] #extracts first row As an interesting fact, you can also create a matrix from a vector. All you need to do is, assign dimension dim() later. Like this: Data Type - R
  • 8. Data Frame: This is the most commonly used member of data types family. It is used to store tabular data. It is different from matrix. In a matrix, every element must have same class. But, in a data frame, you can put list of vectors containing different classes. This means, every column of a data frame acts like a list. Every time you will read data in R, it will be stored in the form of a data frame. Hence, it is important to understand the majorly used commands on data frame: > df <- data.frame(name = c("ash","jane","paul","mark"), score = c(67,56,87,91)) > df name score 1 ash 67 2 jane 56 3 paul 87 4 mark 91 > dim(df) [1] 4 2 str(df) 'data.frame': 4 obs. of 2 variables: $ name : Factor w/ 4 levels "ash","jane","mark",..: 1 2 4 3 $ score: num 67 56 87 91 > nrow(df) [1] 4 > ncol(df) [1] 2 Let’s understand the code above. df is the name of data frame. • dim() returns the dimension of data frame as 4 rows and 2 columns. • str() returns the structure of a data frame i.e. the list of variables stored in the data frame. • nrow() and ncol() return the number of rows and number of columns in a data set respectively. you see “name” is a factor variable and “score” is numeric. In data science, a variable can be categorized into two types: • Continuous and • Categorical. Data Type - R
  • 9. Let’s Create a Vector, Metrics, Dataframe and List • Create list of anything you want • Create a numeric vector 1 to 5 • Create character vector a to d • Create Metrics consist 10 rows and 10 Columns • Create Dataframe of three column to the variable df • First Column name ‘alpha’ and value will be from a to e • Second column name ‘numeric’ and value will be from 1 to 5 • Third column name ‘missing’ and value will be (4,5,2,NA,NA) Data Type – R : Exercise
  • 10. Control structure ‘controls’ the flow of code / commands written inside a function. A function is a set of multiple commands written to automate a repetitive coding task. For example: You have 10 data sets. You want to find the mean of ‘Age’ column present in every data set. This can be done in 2 ways: either you write the code to compute mean 10 times or you simply create a function and pass the data set to it. Let’s understand the control structures in R with simple examples: if, else – This structure is used to test a condition. Below is the syntax: if (<condition>){ ##do something } else { ##do something } Example #initialize a variable N <- 10 #check if this variable * 5 is > 40 if (N * 5 > 40){ print("This is easy!") } else { print ("It's not easy!") } [1] "This is easy!" Control Structure
  • 11. for: This structure is used when a loop is to be executed fixed number of times. It is commonly used for iterating over the elements of an object (list, vector). Below is the syntax: for (<search condition>){ #do something } Example #initialize a vector y <- c(99,45,34,65,76,23) #print the first 4 numbers of this vector for(i in 1:4){ print (y[i]) } [1] 99 [1] 45 [1] 34 [1] 65 while: It begins by testing a condition, and executes only if the condition is found to be true. Once the loop is executed, the condition is tested again. Hence, it’s necessary to alter the condition such that the loop doesn’t go infinity. Below is the syntax: while (<search condition>){ #do something } Example #initialize a condition Age <- 12 #check if age is less than 17 while(Age < 17){ print(Age) Age <- Age + 1 #Once the loop is executed, this code breaks the loop } [1] 12 [1] 13 [1] 14 [1] 15 [1] 16 Control Structure
  • 12. Let’s Install all required Packages using Loop and If Condition # Creating List of packages packages <- list("XLConnect","RODBC","tm","rvest","data.table", "networkD3","webshot","ggplot2") # Creating loop to run through list for( package in packages){ # if condition, to check if package is already installed if (package %in% installed.packages()[,1]){ print(paste(package,"available")) }else{ install.packages(package, dependencies = TRUE, repos="http://cran.rstudio.com/") } } Control Structure : Exercise
  • 13. Let’s now understand the concept of missing values in R. This is one of the most painful yet crucial part of predictive modeling. You must be aware of all techniques to deal with them. Missing values in R are represented by NA and NaN. Now we’ll check if a data set has missing values (using the same data frame df). Create dataframe with NA: > df = data.frame(name=c("a","b","c","d","e","f"),score=c(1,2,3,4,NA,NA)) > df name score 1 a 1 2 b 2 3 c 3 4 d 4 5 e NA 6 f NA > is.na(df) #checks the entire data set for NAs and return logical output name score name score [1,] FALSE FALSE [2,] FALSE FALSE [3,] FALSE FALSE [4,] FALSE FALSE [5,] FALSE TRUE [6,] FALSE TRUE Missing Data
  • 14. > table(is.na(df)) #returns a table of logical output FALSE TRUE 10 2 > df[!complete.cases(df),] #returns the list of rows having missing values name score 5 e NA 6 f NA Missing values hinder normal calculations in a data set. For example, let’s say, we want to compute the mean of score. Since there are two missing values, it can’t be done directly. Let’s see: mean(df$score) [1] NA > mean(df$score, na.rm = TRUE) [1] 2.5 The use of na.rm = TRUE parameter tells R to ignore the NAs and compute the mean of remaining values in the selected column (score). To remove rows with NA values in a data frame, you can use na.omit: > new_df <- na.omit(df) > new_df name score 1 a 1 2 b 2 3 c 3 4 d 4 Missing Data
  • 15. Data frame created in Data Type session, we have ‘NA’ value in ‘missing’ column First print data frame, type df in console Now use: • is.na(dataframe) # on data frame • table(is.na(dataframe) # to know if there is any NA in data frame • na.omit(dataframe) # to remove na from data frame Missing Data : Exercise
  • 16. # Create a folder at some location # Set working directory to the folder setwd(enter_path_here) From EXCEL: #install.packages('XLConnect', dependencies=TRUE, repos='http://cran.rstudio.com/’) library(XLConnect) wb = loadWorkbook(“AAPL.xlsx") ExcelData = readWorksheet(wb, sheet = "Sheet1", header = TRUE) From CSV: CSVData <- read.csv(file="AAPL.csv", header=TRUE, sep=",") From Database (SQL Database): #install.packages("RODBC", dependencies=TRUE, repos='http://cran.rstudio.com/') library(RODBC) conn <- odbcDriverConnect('driver={SQL Server};server=PRANAVSQLEXPRESS;database=CentralMI;trusted_connection=true’) SQLData <- sqlQuery(conn, "SELECT * FROM requestdetail;") close(conn) Read Data
  • 17. From WEB: #install.packages('rvest',dependencies = TRUE,repos = 'http://cran.rstudio.com') library(rvest) url <- "https://www.iplt20.com/stats/2018" webpage <- read_html(url) webpage1 <- webpage%>%html_nodes("table")%>%.[1]%>%html_table(fill=TRUE) webpage2 <- data.frame(webpage1) Inbuilt Data: data(mtcars) From PDF: #install.packages('tm',dependencies=TRUE, repos='http://cran.rstudio.com/') library('tm') file <- 'namefile.pdf' Rpdf <- readPDF(control = list(text = "-layout")) corpus <- VCorpus(URISource(file), readerControl = list(reader = Rpdf)) corpus.array <- content(content(corpus)[[1]]) Read Data
  • 18. Steps to Manipulate: 1) Import data 2) Sub-setting data (removing unwanted data) 3) Selecting required column 4) Selecting required row 5) Merging other data for mapping 6) Grouping / aggregate 7) Exporting data to various output (1) Import Data (3) Sub-setting data (removing unwanted data) # now we required cars having 3 gear table1 <- subset(table,table[,gear==3]) (3) Selecting required columns: # We will select columns from 1 to 3 and 10 to 12 table2 <- table1[,c(1:3,10:12)] (5) Merging other data for mapping # Create mapping dataframe mapping <- data.frame(carb=c(1,2,3,5),name=c("a","a","b“,”c”)) Inner join: merge(x = table3, y = mapping) Outer join: merge(x = table3, y = mapping, by='carb', all = TRUE) Left outer: merge(x = table3, y = mapping, by='carb', all.x = TRUE) Right outer: merge(x = table3, y = mapping, by='carb', all.y = TRUE) Cross Join: merge(x = table3, y = mapping, by=NULL) # Let’s create inner join and assign to variable named ‘table4’ table4 <- merge(x = table3, y = mapping) data(mtcars) # let’s understand data, have name. head(mtcars) # You will find first column do not have name, So will import library library(data.table) table <- data.table(mtcars) head(table) (4) Selecting required rows: # We will select columns from 1 to 3 and 10 to 12 table3<- table2[c(1:10),] Manipulate Data
  • 19. (6) Grouping/ Aggregate # Sum of ‘mpg’ column on newly merged column ‘name’, table5 <- aggregate(table4$mpg, by=list(Category=table4$name), FUN=sum) # Max of ‘mpg’ column on newly merged column ‘name’, Max <- aggregate(table4$mpg, by=list(Category=table4$name), FUN=max) # Min of ‘mpg’ column on newly merged column ‘name’ Min <- aggregate(table4$mpg, by=list(Category=table4$name), FUN=min) # Mean of ‘mpg’ column on newly merged column ‘name’, Mean <- aggregate(table4$mpg, by=list(Category=table4$name), FUN=mean) # Standard Deviation of ‘mpg’ column on newly merged column ‘name’, Sd <- ggregate(table4$mpg, by=list(Category=table4$name), FUN=sd) # Standard Deviation of ‘mpg’ column on newly merged column ‘name’, Median <- aggregate(table4$mpg, by=list(Category=table4$name), FUN=median) # Standard Deviation of ‘mpg’ column on newly merged column ‘name’, Summary <- summary(table4) (7) Exporting data in CSV file: write.csv(Summary, file = "F:R Programmingtest.csv", row.names = FALSE) Manipulate Data
  • 20. To CSV: write.csv(x, file = "F:R Programmingtest.csv", row.names = FALSE) To Excel: #install.packages('XLConnect', dependencies=TRUE, repos='http://cran.rstudio.com/’) library(XLConnect) wb = loadWorkbook(“AAPL1.xlsx") writeWorksheetToFile(wb , data = data , sheet = "sheet1", startRow = 1, startCol = 1) Write Data
  • 21. The different parts of a function are − Function Name − This is the actual name of the function. It is stored in R environment as an object with this name. Arguments − An argument is a placeholder. When a function is invoked, you pass a value to the argument. Arguments are optional; that is, a function may contain no arguments. Also arguments can have default values. Function Body − The function body contains a collection of statements that defines what the function does. Return Value − The return value of a function is the last expression in the function body to be evaluated. R has many in-built functions which can be directly called in the program without defining them first. We can also create and use our own functions referred as user defined functions. Function # Create a function with arguments. myfunction <- function(a,b,c) { result <- a * b + c print(result) } # Call the function by position of arguments. myfunction(10,11,22) # function to take user input readinteger <- function(){ n <- readline(prompt="Enter an integer: ") return(as.integer(n)) } print(readinteger()) # print input # syntax of function myfunction <- function(arg1, arg2, ... ){ statements return(object) }
  • 22. function1 = function(path='F://R Programming',filename='AAPL.xlsx’,outputfilename='AAPL1.xlsx’,sheetname=‘Sheet1',skiprows=0,groupby=none,aggregate=sum) { # joining path and file name filedetail = paste(path, "/",filename,sep="") outputdetail = paste(path, "/", outputfilename,sep="") # if condition to check, package is already installed if('XLConnect' %in% installed.packages()[,1]) { }else{install.packages('XLConnect', dependencies=TRUE, repos='http://cran.rstudio.com/’)} # importing library library(XLConnect) # Loading workbook wb = loadWorkbook(filedetail) # Reading data from workbook df = readWorksheet(wb , sheet = sheetname, header = TRUE) # select few top records for display head(df) # Summary of dataset summary = summary(df) # write to Excel file writeWorksheetToFile(outputdetail , data = summary , sheet = "sheet1", startRow = 3, startCol = 4) } #Calling function function1() Function – Build Own Function
  • 23. setwd() # Let’s create chart with built in data set # Step 1) Import data data(mtcars) # load library library(data.table) # use data.table library to get name of first column table = data.table(mtcars, keep.rownames=TRUE) # select 1st and 2nd column for charts table1 = table[,1:2] # Give the chart file a name png(file = "barchart_Cars_mpg.png") # data is ready for charts h <- table1$mpg y <- table1$rn x <- table1$mpg barplot(h,xlab="Cars",ylab="mpg",names.arg=y,,col="red",main=" Cars-mpg",border="red") dev.off() #syntax of barplot barplot(H,xlab,ylab,main, names.arg,col) • H is a vector or matrix containing numeric values used in bar chart. • xlab is the label for x axis. • ylab is the label for y axis. • main is the title of the bar chart. • names.arg is a vector of names appearing under each bar. • col is used to give colors to the bars in the graph. Charts
  • 24. if('networkD3' %in% installed.packages()[,1]) { }else{install.packages('networkD3', dependencies=TRUE, repos='http://cran.rstudio.com/')} if('webshot' %in% installed.packages()[,1]) { }else{install.packages('webshot', dependencies=TRUE, repos='http://cran.rstudio.com/')} # Load package library(networkD3) library(webshot) # create data: set.seed(101) links=data.frame( source=c("A","A", "A", "A", "A","J", "B", "B", "C", "C", "D","I"), target=c("B","B", "C", "D", "J","A","E", "F", "G", "H", "I","I") ) # Plot graph = simpleNetwork(links, Source = 1, # column number of source Target = 2, # column number of target height = 480, # height of frame area in pixels width = 480, linkDistance = 120, # distance between node. Increase this value to have more space between nodes charge = -480, # numeric value indicating either the strength of the node repulsion (negative value) or attraction (positive value) fontSize = 22, # size of the node names fontFamily = "serif", # font og node names linkColour = "#666", # colour of edges, MUST be a common colour for the whole graph nodeColour = "red", # colour of nodes, MUST be a common colour for the whole graph opacity = 0.8, # opacity of nodes. 0=transparent. 1=no transparency zoom = T # Can you zoom on the figure?) saveNetwork(graph,file = '#252_interactive_network_chart1.html',selfcontained = T) Network chart with D3 using R – Sample 1
  • 25. # libraries library(networkD3) # Load data data(MisLinks) data(MisNodes) # Plot forceNetwork( Links = MisLinks, Nodes = MisNodes, Source = "source", Target = "target", Value = "value", NodeID = "name", Group = "group", opacity = 0.8, linkDistance = JS('function(){d3.select("body").style("background-color", "#DAE3F9"); return 50;}') ) Network chart with D3 using R – Sample 2