0% found this document useful (0 votes)
19 views41 pages

R Programming

R - Data fram

Uploaded by

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

R Programming

R - Data fram

Uploaded by

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

R

Languag
VIT
UNIVERSITY
CHENNAI

e CAMPUS
Introduction

The R system for statistical computing is an environment for data


analysis and graphics.
The root of R is the S language.
It is developed by John Chambers and colleagues at Bell
Laboratories(1960).
Installation of R
R has a command line interface
R is a programming Language and Rstudio (IDE) is an interface for
R
Rstudio Layout

Rstudio Layout

Console window:
It is situated at Bottom Left of the RStudio Layout
It is also called command window
We can write commands
All commands can be executed in this window only
Rstudio Layout

Editor window:
It is situated at Top Left of corner of the RStudio Layout
We are using this window for writing scripts  collection of
commands
It is also called script window
If this window is not visible, we can get it by File  New 
Rscript
Click RUN or CRTL+ENTER to send the highlighted commands
to command window
Rstudio Layout

History window:
It is situated at Top Right of corner of the RStudio Layout
In this window, we can see the data and values of R in
memory.
It is also called workspace window
We can view and edit the values by clicking on them
This history window shows what has been typed so far
Rstudio Layout

Help window:
It is situated at the right bottom of RStudio Layout
Here we can open files and view plots
We can install and load the packages
Short-cut Keys in Rstudio
Setting the Working Directory in
RStudio
Setting the working directory:
To store working file.
create a folder and named as RdataWork.
To create working directory  setwd(“path”).
setwd(“E:/studies/VIT/R/RdataWork”) in windows environment
setwd(“~/RdataWork/”) in Linux environment
R as a Simple Calculator

R as a simple Calculator:
Typing in a mathematical expression and hitting enter prints
out the result.
> 1 + 2
[1] 3
Order of operation rules worked as expected
Mathematical functions such as the square root sqrt
> sqrt(36)
[1] 6
R as a Simple Calculator

R as a simple Calculator:
The result of mathematical expression can be assigned to an
object in R.  <- Operator
var1 <- sqrt(81)
var1
Every object in R belongs to a class  type of the object it
represents
class(var1)
[1] “numeric”
Everything in R is an object, including functions.
ls()  list objects  prints the all the objects.
Operation Symbols in R

Operation Symbols in
R: Symbol Meaning

+ Addition

- Subtraction

* Multiplication

/ Division

%% Modulo (estimates remainder in a


division)
^ Exponential
Numbers in R
Numbers in R:
NAN (not a number)
NA (missing value)
Basic handling of missing values
> var2 =c(1,2,3,4,5,6,NA)
> var2
[1] 1 2 3 4 5
6 NA
> mean(var2)
[1] NA
> mean(var2,na.rm=TRUE)
[1] 3.5
Variable assignment in R

Variable assignment in R:
> var10 = 25
[1] 25
Var11 <- 29
[1] 29
Variable assignment in R
Variable assignment in
R:> var10 =
25
>var10
[1] 25 ;; typeof(var11)  check
>var11 <- result
29
 we can create a list using c-
>var11
command
[1] 29
var22 <- c(1,2,3,4)
mean(var22)  check the output
var(var22)  check the output
Basic Data Types in R

Basic Data types in


R:
Numeric
Integer
Complex
Logical
Character
Vector
Matrix
List
Data Frame
Basic Data Types in R

Numeric in R:
> var12 = 25.12
>var12  check output
is.integer()  used to check wheater a given variable object
is integer or not
is.integer(var12)  check output

typeof(var12)  check output


Basic Data Types in R
Integer in R:
To create an integer variable in R  as.integer().
>var23 <- as.integer(999)
> var23  check output
is.integer(var23)  check output
typeof(var23)  check output
class(var23)  check output
var34 <- as.integer(10.28)
var34  check output
is.integer(var34)  check output
typeof(var34)  check output
Basic Data Types in R

Logical values in R: (and (&), or (|) ,


Negation(!)
True  1 and False  0
> as.integer(TRUE)
> 1
> as.integer(FALSE)
0
 >x = 1
 >y = 2
>z = x > y
>z  check the output
class(z)  check the output
Basic Data Types in R

complex values in R:
A complex value in R is defined via the pure imaginary value
i.
 > z = 1 + 2i
> z
[1] 1+2i
class(z)  check output
sqrt(-1)  check output
sqrt(as.complex(-1))  check the output
Basic Data Types in R
Vector in R:
A vector is a sequence of data elements of the same basic type.
components  members in a vector
Examples
 >c(10,20,40)  3 members/ components
> [1] 10 20 40
 >c(TRUE, FALSE, TRUE)  check output
 >c(“VIT”, “Chennai”, “Campus”)  check output
 >c(“VIT”, 600126)  check output
 > class(c(600123,"vit"))  check output
 >length(c(600123,"vit"))  check Output
Basic Data Types in R

Combining Vector in R:
 >var25 = c(1,4,7)
>var26 = c("vit", "university")
 >var27 = c(var25, var26)
>var27
class(var27)  check output
class(var26)  check output
class(var25)  check output
Basic Data Types in R

Vector arithmetic in R:
 > var28 = c(2, 4, 6, 8)
 > var29 = c(2, 4, 6, 8)
> var28 * var29
> var28 + var29
What’s happens if one vector size less compared to other vector 
Recycling rule
 > var30 = c(2,4)
> var28 + var30  check the result
What happens if one vector is numeric and other is character  able to
perform arithmetic operation these two vectors  No. why
Basic Data Types in R
Vector index in R:
> var33 = c (“vit”, “chennai”, “vellore”, “campus”)
> var33[1] Check the output
We can retrieve the values in a vector, using indices, that has to used in array
bracket. Index is starting from 1.
The result of the slice is also a vector.
Negative Index:
If the index is negative, it would strip the member whose position has the same absolute value as
the negative index.
 >var33[-2]  it skip the values at 2nd position in vector
Out of Range index:
If an index is out-of-range, a missing value will be reported via the symbol NA.
 > var33[10]  check the output
Basic Data Types in R

Numeric index Vector in R:


A new vector can be sliced from a given vector with a numeric index vector,
which consists of member positions of the original vector to be retrieved.
> var33[c(3,4)]  check output
Duplicate Indexes:
 > var33[c(3,4,4)]
Out of Order Indexes:
 >var33[c(4,3,1)]
Range – Index
 > var33[1:3]
Basic Data Types in R

Named Vector in
R:We can assign names to vector
members
>var33
> names(var33) = c(“one”, “two”, “three”, “four”)
>var33  check the output.
>var33[“two”]  check the output
Basic Data Types in R

Matrix in R:
A matrix is a collection of data elements arranged in a two-dimensional
rectangular layout.
Example:
> A = matrix(c(2,4,1,3,5,7),nrow=2,ncol=3,byrow=TRUE)
> A  check the output
> A[2,3]  check the output
> A[2, ]  check the output
> A[, 3]  check the output
> A[, c(1,3)]  check the output
 > dimnames(A) = list( c(“r1” ,”r2”), c(“c1”,”c2”,”c3”) )
> A  check output
Basic Data Types in R

Matrix in R:
A matrix is a collection of data elements arranged in a two-
dimensional rectangular layout.
Example:
> K <- t(A)  check output  transpose of A
 > B = matrix( c(10,20,30), nrow=3, ncol = 1)
> cbind(K,B)  check output  cbind() used for combining matrices by
columns
> rbind()  use to combine matrices based on row-wise
c(B)  destruction the matrix form a vector
Basic Data Types in R

Lists in R:
A list is a generic vector containing other objects.
Example:
 > n = c(2, 3, 5)
 > s = c("aa", "bb", "cc", "dd", "ee")
> b = c(TRUE, FALSE, TRUE, FALSE, FALSE)
> x = list(n, s, b, 3)
x  check the output
x[2]  check the output
x[c(2,4)]  check the output
Basic Data Types in R

Lists in R:
Member Reference:
Example:
 > x[ [2] ]  check the
output
 > x[ [2]] = “ra”
 > x[ [2]]  check the output
 > s  check the output
Basic Data Types in R

Lists in R:
Named List Member:
 We can assign names to list members, and reference them by names instead of
numeric indexes.
 > v = list(bob=c(2, 3, 5), john=c("aa", "bb"))
>v  check the output
 > v[“bob”]
 > v[c(“bob”, “john”)]
> v[["bob"]]  check the output
> v$bob  check the output
Basic Data Types in R

attach() and detach() in R:


We can attach a list to the R search path and access its members
without explicitly mentioning the list.
It should to be detached for clean up.
 example:
 > attach(v)
 > bob  check output
 detach(v)
Basic Data Types in R

Data frame in R:
A data frame is used for storing data tables.
It is a list of vectors of equal length.
Example:
 > n = c(2, 3, 5)
 > s = c("aa", "bb", "cc")
 > b = c(TRUE, FALSE, TRUE)
 > df = data.frame(n, s, # df is a data
b) frame
Basic Data Types in R
Data frame in R:
Build – in Data Frames:
Example:  > mtcars
> mtcars  check the output
Header, data row or instances, cell or variable
mtcars[1,2]  give the cell value of first row and 2nd column.
> mtcars["Mazda RX4", "cyl"]  check output
> nrow(mtcars)  check output
> ncol(mtcars)  check the output
> help(“mtcars”)
> head(mtcars)  check output
> tail(mtcars)  check output
>dim(mtcars)  check output
Basic Data Types in R

Data frame column vector in


R:
uses [[]]  data frame column
Example:
 >mtcars[[6]]
 > mtcars[[“am”]]
 >mtcars$am
 > mtcars[, “am”]
Basic Data Types in R

Data frame column slice in


R:  >mtcars[1]
 > mtcars[“mpg”]
> mtcars[c(“mpg”,
“hp”]
Basic Data Types in R

Data frame Row slice in


R: Example:
 >mtcars[24,]
 > mtcars[c(3,24)]  to retrieve more than one row
data
Data Import in R

Data import in R:
Import of xls sheets
Code -1:
> library(gdata)
> mydata = read.xls("mydata.xls")
Code – 2:
 > library(XLConnect)
 > wk = loadWorkbook("mydata.xls")
 > df = readWorksheet(wk,
sheet="Sheet1")
library(xlsx)

Data Import in R

Data import in R:
Import of xls sheets
Code – 3 :
> library(xlsx)
> mydata <- read.xlsx("c:/myexcel.xlsx", 1)
> mydata <- read.xlsx("c:/myexcel.xlsx", sheetName = "mysheet")
Data Import in R

Data import in R:
Import of table file:
Code -1:
> mydata = read.table(“~~path/mydata.txt")
> mydata
Code – 2:
> mydata =
read.table(file.choose(),header=TRUE/FALSE)
 > mydata
 >help(read.table)  see the output
Data Import in R

Data import in R:
Import of CSV file:
Code -1:
> mydata = read.table(“~~path/mydata.csv")
> mydata
Code – 2:
> mydata =
read.table(file.choose(),header=TRUE/FALSE, sep=“,” ,
row.names=“id”)
 > mydata

You might also like