Matrix, Dataframes, List
Matrix, Dataframes, List
firsttwo<-order_detail[1:2]
firsttwo
l<-length(order_detail)
l
v1<-order_detail[(l-1):l]
v1
v2<-order_detail[(l-1):2]
v2
v3<-order_detail[l:1]
v3
v4<-order_detail[(l-2):l]
v
(order_detail %% 3)==0
order_detail[(order_detail %% 3)==0]
na.omit(order_detail[(order_detail %% 3)==0])
mean(order_detail,na.rm=T)
max(order_detail,na.rm=T)
min(order_detail,na.rm=T)
sd(order_detail,na.rm=T)
sqrt(order_detail) # returns the square root
##-----------Matrix in R-------------------##
# We use function matrix() to create a matrix
v <- 20:30
v
matrix(v)
mat1<-matrix(0,3,3)
mat1
mat2<-matrix(1:9,3,3)
mat2
mat3<-matrix(1:9,nrow=3,byrow=T)
mat3
mat3<-matrix(1:9,3,3,byrow=T)
mat3
stock.matrix
# Matrix addition
5+mat1
# Matrix multiplication
5*mat1
# Division of matrix
2/mat1 # returns the reciprocal
# Division of matrix
# Exponent
mat1 ^ 2
mat1 + mat1
mat1-mat1
mat1*mat1
# Matrix operations
# Adding rows and columns to a matrix using rbind() and cbind() function
student<-
matrix(c(20,30,NA,70,22,28,36,80,24,26,32,75,26,24,NA,50),nrow=4,ncol=4,b
yrow=T)
dimnames(student)<-
list(c("John","Mathew","Sam","Alice"),c("Phy","Chem","Bio","Maths"))
student
# Extraction of columns
student[,1]
student[,1:2]
student[,c(1,3)]
# Extraction of rows
student[1,]
student[1:2,]
student[c(1,3),]
student[2,2]
student[2,2:4]
student[3:4,2:3]
student[2:4,c(1,4)]
#Find the average and the total score for all the students
apply(student,1,mean,na.rm=T) #here 1 stands for the row and 2 is for
columns
help(apply)
apply(student,1,sum,na.rm=T)
passing_score<-c(25,25,25,70)
passing_score
#----------DataFrames in R---------------#
data()
data(AirPassengers)
head(AirPassengers)
data(iris)
head(iris)
View(iris)
View(state.x77)
View(USPersonalExpenditure)
tail(USPersonalExpenditure)
# Dataframe indexing
# Use dollar sign if you want all the values of a particular column
df$days
df$rain
desc.temp <- order(-df['temp']) # Returns the all the columns with temp
sorted in descending order
df[desc.temp,]
authors
books
salesreport<-data.frame(Id=101:110,
Product=c("A","B"),
Unitprice=as.integer(runif(10,100,200)),
Qty=as.integer(runif(10,10,20))
)
salesreport
salesreport[order(salesreport$Product,-salesreport$Unitprice),]
subset.ProductA<-subset(salesreport,Product=="A")
subset.ProductA
# Extact the rows for which Product is A and Unitprice > 150
subset.salesreport<-subset(salesreport,Product=="A" & Unitprice>150)
subset.salesreport
# Extract only the first and the fourth column Product is A and Unitprice
> 150
subset.salesreport<-subset(salesreport,Product=="A" &
Unitprice>150,c(1,4))
subset.salesreport
setA<-subset(salesreport,Product=="A")
setB<-subset(salesreport,Product=="B")
setA
setB
cbind(setA,setB)
rbind(setA,setB)
#5.Aggregate function
#Total quantity sold for each product
aggregate(salesreport$Qty,list(salesreport$Prod),sum,na.rm=T)
#----------------------Lists in R------------------#
list_1<-list(x=c(10,20,30),
y=c("a","b","c"),
z=c(TRUE,FALSE))
list_1
list2<-list(vec=seq(1:10),
mat=matrix(1:9,3,3),
lis=list(a=10,b=20))
list2
# Indexing of list
list2[2] # By index
list2$mat # By dollar notation
list2['vec'] # By name
list2$lis[2] #3rd element 2nd value
list2[[3]][2] #3rd element 2nd value
length(list2)
class(list2)
price<-c(10,20,30)
pricelist<-as.list(price)
pricelist
price
newPrice<-unlist(pricelist)
newPrice
price1<-c(10,20,30,40)
dim(price1)<-c(2,2)
price1
#All integers are numeric but all numerics are not integers.
is.numeric(I1) #TRUE
is.integer(I1) #TRUE
c1<-"John"
c2<-"Rob"
class(c1)
class(c2)
is.character(c1)
is.character(c2)