Statistic And R Programming lab Exercise
Statistic And R Programming lab Exercise
v1 =c(1:20)
v1
v2 =seq(20,1,-1)
v2
v3=c(1:20, seq(19,1,-1))
v3
temp=c(4,6,3)
temp
# Answer (2)
#2(e)
ve=rep(c(4,6,3),10)
ve
#2(f)
temp=c(4,6,3)
rep(temp,times=11,length.out=31)
#2(g)
vg=c(rep(4,10),rep(6,20),rep(3,30))
vg
#or we can do this in 2nd way
temp=c(4,6,3)
rep(temp,c(10,20,30))
# Answer (3)
x =seq(3,6,by = 0.1)
v2 =exp(x) * cos(x)
v2
# Answer (4)
heights=c(180,165,160,193)
weights=c(180,165,160,193)
bmi=c(weights/(heights^2))
print(bmi)
# Answer (5)
temp=c(23,27,19)
f=(9/5*temp)+32
print(f)
# Answer (6)
set.seed(75)
aMat=matrix (sample(10, size=60, replace=T), nrow=6)
aMat
#Answer 6)a
apply(aMat, 1, function(x){sum(x>4)})
#Answer 6)b
which( apply(aMat,1,function(x){sum(x==7)==2}) )
#Answer 6)c
aMatColSums =colSums(aMat)
aMatColSums
which( outer(aMatColSums,aMatColSums,"+")>75, arr.ind=T )
#or
#Answer (7)
Number = c(1,2,3,4)
Diet = c("Poor","Poor","Good","Good")
Sex = c("M","F","M","F")
Weight= c(156,180,167,190)
fat.content= c(34,43,40,50)
Morph = c("Winged","Winged","Wingless","Intermediate")
#Answer (8)
#class(): Determine the kind of variable
#mode() : a mutually exclusive classification of objects according to their basic structure
#typeof(): Data type of an object
#is() : to check any variable of certain data type
#rm() function is used to delete objects from the memory
v=cbind(c(1,2,3),c(4,5,6))
v
class(v) #class will be matrix and array
mode(v) # mode will be numeric
typeof(v) # typeof will be double
is.array(v) # will return TRUE
is.matrix(v) #will return TRUE
rm(v)
v #object not found
#Answer (9)
fy = c(2010, 2011, 2012, 2010, 2011, 2012, 2010, 2011, 2012)
fy
company =c("Apple", "Apple", "Apple", "Google", "Google", "Google", "Microsoft", "Microsoft",
"Microsoft")
company
revenue = c(65225, 108249, 156508, 29321, 37905, 50175, 62484, 69943, 73723)
revenue
profit =c( 14013, 25922, 41733, 8505, 9737, 10737, 18760, 23150, 16978)
profit
companiesData=data.frame(fy,company,revenue,profit)
companiesData
write.csv(companiesData, file="ray.csv")
head(companiesData)
dim(companiesData)
nrow(companiesData)
ncol(companiesData)
str(companiesData)
min(companiesData$profit)
max(companiesData$profit)
subset(companiesData, fy >=2011, select=c(fy,profit))
#Answer (10)
gender <- factor(c(rep("female", 91), rep("male", 92)))
table(gender)
gender <- factor(gender, levels = c("male", "female")) # mapping of male to female and female to male
table(gender)
gender <- factor(gender, levels = c("Male", "female")) #mapping of Male to male and female to female
Male value will be zero
table(gender)
rm(gender)
gender # object not found because of rm(gender)code
#Answer (11)
for (i in 1:7){
print(i**3)
}
#Answer (12)
#Answer (13)
mx = matrix(c(1,1,3,5,2,6,-2,-1,-3), nrow = 3, byrow = TRUE)
fun = function(mx) {
ifelse(mx %% 2 == 0, mx, 2*mx)
}
res <- fun(mx)
res
#Answer (14)
num=c(19)
#num=as.numeric(readline(prompt = "Enter The number: "))
is.prime <- function(num) {
if (num == 2) {
print(" Number is Prime")
} else if (any(num %% 2:(num-1) == 0)) {
print(" Number is not Prime")
} else {
print(" Number is Prime")
}
}
is.prime(num)
#Answer (15)
data = airquality
print(class(data))
result = data[order(data[,1]),]
print("Order the entire data frame by the first and second column:")
print(result)
#Answer (16)
data(mtcars)
head(mtcars)
#16)a
print("Original dataframe:")
print(mtcars)
print("Structure of the said data frame:")
print(str(mtcars))
#16)b
print("Original dataframe:")
print(mtcars)
print("Statistical summary and nature of the data of the said dataframe:")
print(summary(mtcars))
head(mtcars)
#16)c
print(" to extract specific column from a data frame using column name:")
data.frame(mtcars$mpg)
data.frame(mtcars$mpg,mtcars$gear)
#16)d
print("Original dataframe:")
print(mtcars)
print("Extract first two rows:")
result = mtcars[1:2,]
print(result)
#16)e
print("Original dataframe:")
print(mtcars)
print("Extract 3rd and 5th rows with 1st and 3rd columns :")
result = mtcars[c(3,5),c(1,3)]
print(result)
#16)f
print("Original dataframe:")
print(mtcars)
print("New data frame after adding the 'country' column:")
mtcars$country =
c("USA","USA","USA","USA","USA","USA","USA","USA","USA","USA","India","USA","India","USA","India","
USA","India","USA","USA","USA","India","USA","India","USA","India","USA","India","USA","India","USA","In
dia","USA")
print(mtcars)
#16)g
Alto = data.frame(
#16)h
print("Original dataframe:")
print(mtcars)
print("After removing col(s) to an existing data frame:")
mtcars = subset(mtcars, select = -c(cyl, qsec))
print(mtcars)
#16)i
print("Original dataframe:")
print(mtcars)
print("After removing row(s) to an existing data frame:")
print(mtcars[-c(33,34), ])
#16)j
print("to sort a given data frame by multiple column(s):")
#16)k
print("Original dataframe:")
print(mtcars)
print("Change column-name 'mpg' to 'MPG' of the said dataframe:")
colnames(mtcars)[which(names(mtcars) == "mpg")] = "MPG"
print(mtcars)
#Answer 17)
library(tidyverse)
df1
subset1<-df1[1:5,2:4]
subset1
subset2<-df2[6:10,1:3]
subset2
subset1
names(subset1)<-c("caste","10k","20k")
subset1
subset2
names(subset2)<-c("ID","caste","10k")
subset2
md<-merge(subset1,subset2,all = TRUE)
md
md$ID<-NULL
head(md)
write.csv(md,file = "myd.csv")
#Answer 18)
volumes<-c(1/3*pi*R^2*H)
volumes
#Answer 19)
mean(volumes)
median(volumes)
sd(volumes)
vol<- subset(volumes,H<8.5)
vol
mean(vol)
#Answer 20)
rainforest<-read.csv("rainforest.csv",header = TRUE)
rainforest
Acmena <- subset(rainforest, species == "Acmena smithii")
Acmena
order1 <- order(Acmena$dbh)
order1