Lab 02 - Compound Data Structures
Lab 02 - Compound Data Structures
class(a)
b=c("Dog","Cat","Rat")
b
class(b)
x=c(12,22,23,"Man","Car")
x #This is called the coercion
#logical < integer < numeric < complex < character (This is the order)
class(x)
y=c(TRUE,FALSE,12)
y
class(y)
p=15:5
p
k=seq(from=10,to=50,by=5)
k
h=seq(from=10,to=50,length.out = 20)
h
f=seq_len(20)
f
h=replicate(10,c("Dog","Cow"))
h
a[c(1,3,6)]
a[1:3]
a[a>40]
a[a%%2==0]
a+10
a*2
a/3
a^2
a>3
a+b
a*b
a/b
a^b
a>b
p+q
summary(a)
str(a)
min(a)
max(a)
sum(a)
mean(a)
median(a)
var(a)
sd(a)
range(a)
#Cumulative functions
cumsum(a)
cumprod(a)
cummin(a)
cummax(a)
a=c(20,30,40)
rep(a,3)
b=c(10,20,25,30,35,40)
all(b>20)
all(b<50)
any(b>20)
any(b>50)
b=c(100,50,25,30,15,40,15,40)
sort(b)
order(b)
rev(b)
unique(b)
h=c(12,22,23,34,45,43,32,33,21,23,45,56,67,56,70)
s=sample(h,5)
s
set.seed(1000)
h=c(12,22,23,34,45,43,32,33,21,23,45,56,67,56,70)
s=sample(h,5)
s
p=sample(h,5,replace = T)
p
f=c(12,22,23)
q=sample(f,10,replace = T)
q
k=c(20,12,25,30,32,22,33)
which(k>=25)
which.min(k)
which.max(k)
a=c(12,22,23,34,45,56,43,43,33,34)
idx=which(a%in%c(22,23,34))
b=a[-idx]
b
g=c("Male","Female","Female","Female","Male")
table(g)
g=c("Male","Female","Female","Female","Male")
replace(x = g,list = c(1,5),values = "M")
#Matrices
#Matrix creation
a=matrix(c(12,23,32,33,45,43),3,2)
a
b=matrix(c(12,23,32,33,45,43),3,2,byrow = TRUE)
b
c=array(c(12,23,32,33,45,43),c(3,2))
c
a=matrix(10,3,2)
a
#Matrix properties
b=matrix(c(12,23,32,33,45,43),3,2,byrow = TRUE)
b
dim(b)
nrow(b)
ncol(b)
str(b)
summary(b)
#Merging matrices
a=matrix(c(12,23,32,33),2,2)
b=matrix(c(22,26,37,43),2,2)
c=cbind(a,b)
c
d=rbind(a,b)
d
a[1,3]
a[1,]
a[,2]
a[c(1,3),2]
a[,2:3]
a>40
a[a>40]
#Matrix operations
a=matrix(c(12,23,32,33),2,2)
b=matrix(c(22,26,37,43),2,2)
c=matrix(c(12,23,32,33,45,43,34,55,56),3,3)
b
a+b
a-b
a*b
a/b
a%*%b
t(c)
det(c)
solve(c)
diag(c)
#Lists
#Creating a list
a=c(12,22,23,34)
b=c("Cat","Dog")
c=100
d="Man"
m=matrix(c(12,22,23,34,45,32),3,2)
L=list(a,b,c,d,m)
L
L$Part1
#Merge lists
L1=list("Man",c(12,22,23),TRUE)
L2=list(matrix(1:20,4,5),c("Cat","Dog"))
L1
L2
L3=c(L1,L2)
L3
#Splitting a list
a=c(12,22,23,34)
b=c("Cat","Dog")
c=100
d="Man"
m=matrix(c(12,22,23,34,45,32),3,2)
L=list(a,b,c,d,m)
L
L1=L[1:2]
L2=L[3:5]
L1
L2
#Factors
#Un Ordered factors
g=c("Male","Female","Male","Male","Male","Female")
g
fg=factor(g)
fg
#Ordered factors
h=c("first","first","fifth","fourth","second","fifth","third","second","fourt
h")
h
fh=factor(h)
fh
ofh=factor(h,levels = c("first","second","third","fourth","fifth"))
ofh
#Data Frames
name=c("Kane","Jane","David","Harry","Larry","Mary","John","Jessy","Anne","Li
lly","Julia","Pale")
age=c(23,33,34,32,21,22,23,34,32,18,21,23)
marks=c(89,78,88,59,67,78,88,90,59,75,77,69)
df=data.frame(name,age,marks)
df
df["age"]
name
age
marks
df[1,1]="Sammie"
df
df[2:6,2]=c(50,50,50,50,50)
df
df$marks_diff=df$marks_new-df$marks
df
#Checking conditions
df$marks>=70 #Returns TRUE for marks greater than 70
head(x = df,n = 8)
tail(x = df,n = 8)
dim(df) #Dimensions
nrow(df)
ncol(df)
colnames(df)
colnames(df)=c("Student_Name","Previous_Marks","Class","New_Marks","Marks_Dif
ference")
df
rowMeans(df[c("Previous_Marks","New_Marks")])
#str function will give all the data types in the data frame
str(df)
#which function can be used for identifying the indexes of some criteria
which(df["Previous_Marks"]>=70)
str(df)
summary(df)
df$Gender=factor(df$Gender)
df$University_Year=factor(df$University_Year,levels = c(1,2,3))
str(df)
summary(df)
getwd()
dim(data)
colnames(data)
str(data)
table(data$Loan.Offered)
table(data$Own.house)
data$Gender=factor(data$Gender)
data$Loan.Offered=factor(data$Loan.Offered)
data$Job=factor(data$Job)
data$Status=factor(data$Status)
data$Credit.History=factor(data$Credit.History)
data$Own.house=factor(data$Own.house)
data$Purpose=factor(data$Purpose)
str(data)
head(data)
summary(data)
summary(data_female)
data_female$CS_Ex_Ratio=data_female$Credit.Score/data_female$Work.Exp
head(data_female)
summary(data_male)
data_male$Exp_Level="Low"
head(data_male)
data_male[data_male$Work.Exp>=15,]$Exp_Level="High"
head(data_male)
#Data simulation
datanorm=rnorm(100) #Standard normal distribution
datanorm
datapois=rpois(n = 100,lambda = 5)
datapois
#rnbinom(),rgamma(),rhyper(),rbeta()