You are on page 1of 5

############# Lesson 3 Remaining ########################

#printing text. Use quotes


print("Training for Data Science with R")

a="India"; b="USA" ; c="United Kingdom"


a
b
c

paste(a,b,c) # Concatenation function


#OR
Countries = paste(a,",",b,",",c)
Countries

# Trim function to remove spaces


a= "India "
a
paste(a,b)

Trimmed= trimws(a,which=c("right"))
Trimmed

a= " India "


a
Trimmed= trimws(a,which=c("both"))
Trimmed

# Count of chars function


nchar(a)
nchar(Trimmed)

# nchar will work on numeric as well


b=19090
class(b)
nchar(b)

# length function - number of elements in a vector


x = c(1,2,3,4)
length(x)
nchar(x)

x = c(1,2,3,4,NA)
mean(x,na.rm=T)
length(x)

NameOFEmp = " John"


nchar(NameOFEmp)
numericObject = 1000
nchar(numericObject)

nameOfEmp = c("Jim","Timothy")
nchar(nameOfEmp)
################# Lesson 4 #####################

x = c(1,2,3,4,5)
mean(x)

x = c(1,2,3,4,5,NA)
mean(x,na.rm=TRUE)

# Vector is basically series of elements. Define elements. Vector is 1 Dimensional


# 1. Integer
x= c(1,5,3,67,83,45,09,33) # Integer
x
View(x)
class(x)
length(x)

## Indexing
# extracting an element from vector
x[5]

x[1:4]

x[c(3,6)]
x[-c(3,6)]
x[3:5]

x[-1]
x[-c(1,3)]

# 2. Numeric Real number


y=c(1.5,23.6,12.5) # Numeric Real number
y
class(y)

# 3. Logical
LogicalVector = c(TRUE,FALSE) # Logical Vector
LogicalVector
View(LogicalVector)

############### MATRIX ###############


m = matrix(1:6,nrow=2,ncol=3)
m

m = matrix(1:6,nrow=2,ncol=3,byrow=T)
m
#OR
mm = matrix (c(1,6,3,8,7,5),nrow=2,ncol=3)
mm
mm[1,3]

mm[,1]
mm[2,]

m
t(m) # transpose
is.vector(m)
is.matrix(m)

rowSums(m)
colSums(m)
rowMeans(m)
colMeans(m)

x = c(1,2,2,3,3,4,NA,5,5,5)
unique(x)

# upper function to convert elements to upper case


NameOfEmp = c("Jim","JIM","Timothy","TIMOTHY")
unique(NameOfEmp)

NameOfEmpUpper = toupper(NameOfEmp)
NameOfEmpUpper
unique(NameOfEmpUpper)

NameOfEmpUpper = initcap(NameOfEmp)

################### ARRAYS: More than 2dimentional #############


# For one dimention we use Vector for 2 dim its matrix, for > 2 dim its array
Array_1 = array(1:50,dim=c(5,5,2)) # 5 rows 5 columns and 3rd dimention
Array_1

View(Array_1)

Array_1[1,5,1]

################### DATA FRAME ###################


# Major Benefit of data frame is that it can have lot of variable with different
classes i.e 1 var is num another is char etc.
# Method 1
EmployeeID = c(101,102,103)
EmployeeName = c("Alex","Steve","Jim")
DateOfJoining=c("01sep2006","05Aug2010","21Dec2013")
EmployeeID
EmployeeName
DateOfJoining

Empdata = data.frame(EmployeeID,EmployeeName,DateOfJoining)
Empdata
class(Empdata)
View(Empdata)
# Method 2
EmpData1 = data.frame(
EmployeeID = c(101,102,103),
EmployeeName = c("Alex","Steve","Jim"),
DateOfJoining=c("01sep2006","05Aug2010","21Dec2013")
)

View(EmpData1[,1])

m
class(m)

mIntoDF = as.data.frame(m)
class(mIntoDF)

DFbackToMatrix = as.matrix(mIntoDF)
class(DFbackToMatrix)

# With different elements but with NA


EmpData1 = data.frame(
EmployeeID = c(101,102,103,104),
EmployeeName = c("Alex","Steve","Jim",NA),
DateOfJoining=c("01sep2006","05Aug2010","21Dec2013",NA)
)
str(EmpData1)
View(EmpData1)

EmpData1[,1]
# OR
EmpData1$EmployeeID
EmpData1$EmployeeName

EmpData1
EmpData1[1,2]

## creating a list
MyList = list(34,"Lisa","02-Oct-2016")
MyList
class(MyList)
MyList[1]

MyList2D = list(Emp1 = c(34,"Lisa","02-Oct-2016"),Emp2 = c(34,"Lisa","02-Oct-


2016"))
MyList2D

# importing excel data


library(gdata)

library(XLConnect)
XLOutput = readWorksheet("Electronic.xlsx",sheet = "Sales")

EmpData1
write.csv(EmpData1,"EmpDataExprted.csv")

You might also like