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

Lab1

The document contains R code snippets for various tasks including summing elements of a vector, calculating the volume of spheres based on radius, categorizing ages from a CSV file, and performing statistical analysis on data from another CSV file. It also includes a function to compute percentiles from a sorted vector. Each section of code is labeled and demonstrates different programming techniques in R.

Uploaded by

Điền Lê Quan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Lab1

The document contains R code snippets for various tasks including summing elements of a vector, calculating the volume of spheres based on radius, categorizing ages from a CSV file, and performing statistical analysis on data from another CSV file. It also includes a function to compute percentiles from a sorted vector. Each section of code is labeled and demonstrates different programming techniques in R.

Uploaded by

Điền Lê Quan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

#Bai 8

x <- c(1,2,3,4,5,6,7,8,9)
i <- 1
s <- 0
while (i<=length(x)){
s = s + x[i]
i=i+1
}
print(s)

#Bai 9
volume <- function(radius){
volume <- (rep(0,length(radius)))
i <- 1
while (i <= length(radius)){
volume[i] <- (4*pi*(radius[i])^3)/3
i <- i + 1
}
return(volume)
}
radius <- 3:20
volume1 <- volume(radius)
data.frame(radius, volume1)

#Bai 10
install.packages("readcsv")
data <- read.csv("C:/Users/DELL/Downloads/data01.csv")
Index <- c(rep(0,length(data$Age)))
for (i in 1:length(data$Age)) {
if (data$Age[i] <= 60) {
Index[i] <- 0
} else if (data$Age[i] > 60 & data$Age[i] <= 70) {
Index[i] <- 1
} else if (data$Age[i] > 70 & data$Age[i] <= 80) {
Index[i] <- 2
} else if (data$Age[i] > 80) {
Index[i] <- 3
}
}
print(Index)
#Bai 11
#1
# Đọc dữ liệu từ file CSV
data <- read.csv("C:/Users/DELL/Downloads/data11.csv")
dataframe <- data.frame(data)
#2
thongke <- function(dataframe) {

CCTB <- (dataframe$a + dataframe$b) / 2

total <- sum(dataframe$n)

min <- min(dataframe$a)


max <- max(dataframe$b)

mean <- sum(CCTB * dataframe$n) / total

variance <- sum(dataframe$n * (CCTB - mean)^2) / (total - 1)


return(list(
min = min,
max = max,
trungbinh = mean,
phuongsai = variance
))
}

ketqua <- thongke(dataframe)


print(ketqua)

#Bai 12
phanvi <- function(X,p){
X_sorted <- sort(X)
i <- (p/100)*length(X)
if (i != floor(i)){
i <- ceiling(i)
phanvi = X_sorted[i]
return(phanvi)
} else{
phanvi = (X_sorted[i] + X_sorted[i+1])/2
return(phanvi)
}
}
X <- c(1,2,3,4,5,6,7,8,9,10)
p <- 25
print(phanvi(X,p))

You might also like