0% found this document useful (0 votes)
5 views

Statistic And R Programming lab Exercise

The document contains a series of R programming exercises covering various topics such as vector creation, data manipulation, statistical analysis, and data frame operations. It includes examples of functions, conditional statements, and data visualization techniques. Additionally, it demonstrates how to read, merge, and write CSV files, as well as perform calculations on datasets.

Uploaded by

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

Statistic And R Programming lab Exercise

The document contains a series of R programming exercises covering various topics such as vector creation, data manipulation, statistical analysis, and data frame operations. It includes examples of functions, conditional statements, and data visualization techniques. Additionally, it demonstrates how to read, merge, and write CSV files, as well as perform calculations on datasets.

Uploaded by

pranshujaguri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

# Answer (1)

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)

bmi_25 =subset(weights, bmi > 25)


print(bmi_25)

# 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

#if want to exclude repeats


aMatColSums <- colSums(aMat)
logicalMat <- outer(aMatColSums,aMatColSums,"+")>75
logicalMat[lower.tri(logicalMat,diag=T)] <- F
which(logicalMat, arr.ind=T)

#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")

sdf1= data.frame(Number,Diet, Sex, Weight,fat.content,Morph)


sdf1
is.factor(sdf1$Diet) #is.factor is false it means it is character type

#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)

Same as 8th Question

#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(

mpg = c(21.0, 12.0),


cyl = c(10.5, 9),
disp = c(1, 3),
hp = c(11, 3),
drat = c(12, 3),
wt = c(14, 3),
qsec = c(10.1, 3.0),
vs = c(11.0, 3),
am = c(12, 3),
gear = c(17, 3),
carb = c(12, 3),
country = c('Usa', 'Usa')
)
mtcars = rbind(mtcars, Alto)
print("After adding new row(s) to an existing data frame:")
print(mtcars)

#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):")

print(mtcars[with(mtcars, order(mpg, carb)), ] )

#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)

# merging two csv files


df1 <- read.csv("religion.csv",
header = TRUE,
sep = ",")

df1

df2 <- read.csv("religion.csv",


header = TRUE,
sep = ",")
df2

subset1<-df1[1:5,2:4]
subset1

subset2<-df2[6:10,1:3]
subset2

#b) rename the headers

subset1
names(subset1)<-c("caste","10k","20k")
subset1
subset2
names(subset2)<-c("ID","caste","10k")
subset2

#c) Merge multiple datasets

md<-merge(subset1,subset2,all = TRUE)
md

#d) Delete the last column of resultant dataset

md$ID<-NULL
head(md)

#e) Add a new column as the second column of resultant dataset


ID<-1:10
ID
md<-cbind(md,ID)
View(md)

#f) Save the final data as a csv file using write.table

write.csv(md,file = "myd.csv")

#Answer 18)

R <- c(2.27, 1.98, 1.69, 1.88, 1.64, 2.14)


H <- c(8.28, 8.04, 9.06, 8.70, 7.58, 8.34)

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

You might also like