📚 R Code Vault by MALAK
💠ONE
Copy Code
REGNO <- c(101,102,103,104,105)
STUDENTNAME <- c("Aakash","Sethu","Prahadeesh","Kala","Mala")
TAMIL <- c(67,78,89,45,35)
ENGLISH <- c(56,67,78,89,75)
MATHS <- c(56,35,23,89,78)
SCIENCE <- c(56,67,89,98,76)
SOCIAL <- c(56,67,78,89,45)
df<- data.frame(REGNO, STUDENTNAME, TAMIL, ENGLISH, MATHS, SCIENCE, SOCIAL)
write.csv(df,"E:/2024_2025/Rlab/studentinfo1.csv", row.names = FALSE, col.names = TRUE)
data<- df[-c(5), ]
print("Before Deletion")
print(df)
df<- df[ -c(3), ]
print("After Deletion")
print(df)
💠TWO
Copy Code
getwd()
setwd("E:/2024_2025/Rlab")
getwd()
datas <- read.csv("MARK.csv")
print(datas)
data <- data.frame(
REGNO = 1:4,
Name = c("A", "B", "C", "D"),
TAMIL = c(67, 76, 88, 90),
ENGLISH = c(67, 76, 88, 90),
MATHS = c(67, 76, 88, 90),
SCIENCE = c(67, 76, 88, 90),
SOCIAL = c(67, 76, 88, 90)
)
write.table(data, file = "MARK.csv", row.names = FALSE)
layout(matrix(c(1,1,2,2,3,3,0,4,4,5,5,0), nrow=2, ncol=6, byrow=TRUE), respect=FALSE)
hist(data$TAMIL)
hist(data$ENGLISH)
hist(data$MATHS)
hist(data$SCIENCE)
hist(data$SOCIAL)
💠THREE
Copy Code
set.seed(123)
mean_value <- 5
sd_value <- 2
num_samples <- 1000
random_numbers <- rnorm(num_samples, mean = mean_value, sd = sd_value)
print(head(random_numbers))
hist(random_numbers)
💠FOUR
Copy Code
Z <- rnorm(255, 0, 1)
u <- 0.3
sd <- 0.2
s <- 100
price <- c(s)
a <- 2
t <- 1:256
for(i in Z) {
S = s + s * (u / 255 + sd / sqrt(255) * i)
price[a] <- S
s = S
a = a + 1
}
plot(t, price, main = "Time series stock X", xlab = "time", ylab = "price",
type = "l", col = "blue")
summary(price)
statistics <- c(sd(price), mean(price), (price[256] - price[1]) / price[1] * 100)
names(statistics) <- c("Volatility", "Average price", "Return %")
print(statistics)
💠FIVE
Copy Code
library(GA)
fitness_function <- function(ch) {
n <- binary2decimal(ch)
if (n == 0) return(-1e6)
-abs(exp(1) - (1 + 1/n)^n)
}
gaControl("binary" = list(
selection = "ga_rwSelection",
crossover = "gabin_spCrossover",
mutation = "gabin_raMutation"
))
myga <- ga(
type = "binary",
fitness = fitness_function,
nBits = 10,
popSize = 100,
maxiter = 3000,
pcrossover = 0.8,
pmutation = 0.1,
elitism = 0,
monitor = TRUE
)
summary(myga)
plot(myga)
best_n <- binary2decimal(myga@solution[1, ])
cat("Best n:", best_n,
"\nValue:", (1 + 1/best_n)^best_n,
"\nError:", abs(exp(1) - (1 + 1/best_n)^best_n), "\n")
🚀 Created with passion by balak #KeepCoding