Index
Progs
1. Write a R program for different types of data structures in R
X = c(1, 2, 3, 4, 5)
X
empid = c(1, 2, 3, 4)
empname = c('Shelton', 'Raghu', 'mic', 'Var')
empno = 4
emplist = list(empid, empname, empno)
emplist
Name = c("Shelton", "Raghu", "Michael", "Varad")
Language = c("C++", "C", "R", "Python")
Age = c(21, 19, 20, 21)
df = data.frame(Name, Language, Age)
df
A = matrix(c(1, 2, 3, 4, 5, 6, 7, 8, 9),nrow = 3, ncol = 3)
A
fac = factor(c("Male", "Female", "Female", "Male", "Female"))
fac
2. Write a R program that include variables, constants, datatypes.
x = 13.8
print(x)
print(class(x))
A = 14L
print(A)
print(class(A))
x = 13.4
print(x)
print(class(x))
alphabet = "a"
print(alphabet)
print(class(alphabet))
message = "Welcome to R Programming"
print(message)
print(class(message))
x = 15L
print(typeof(x))
print(class(x))
z = 3e-3
print(z)
print(class(z))
bool1 = TRUE
print(bool1)
print(class(bool1))
bool2 = FALSE
print(bool2)
print(class(bool2))
complex = 3 + 2i
print(complex)
print(class(complex))
3. Write a R program that include different operators, control structures, default values for arguments, returning complex objects *.
v1 = c(1, 2, 3, 4)
v2 = c(4, 3, 2, 1)
cat("Addition of Vectors: ", v1+v2, "\n")
cat("Substraction of Vectors: ", v1-v2, "\n")
cat("Multiplication of Vectors: ", v1*v2, "\n")
cat("Division of Vectors: ", v1/v2, "\n")
a = c(T, F, T)
b = c(F, F, T)
a1 = T
b1 = F
cat("Element-wise AND: ", a&b, "\n")
cat("Element-wise OR: ", a|b, "\n")
cat("Logical AND: ", a1&&b1, "\n")
cat("Logical OR: ", a1||b1, "\n")
mat = matrix((1:4), nrow = 2, ncol = 2, byrow = T)
cat("Matrix A: \n")
mat
cat("Transpose of A: \n")
t(mat)
cat("Product of A and Transpose of A: \n")
prod = mat * t(mat)
prod
cat("Is 1 present in Product of matrix: ", "1" %in% prod, "\n")
x = 5
if(x > 10){
cat("X is greater than 10\n")
} else {
cat("X is less than 10\n")
}
x = 1:4
for (i in x) {
cat("Value of X is : ", i, "\n")
}
div = function(a, b = 3) {
if(a %% b == 0){
return(cat(a, " is divisible by ", b, "\n"))
}
else {
return(cat(a, " is not divisible by ", b, "\n"))
}
}
div(10, 5)
div(12)
x = rep(1:4) + 1i*(4:1)
x
is.complex(x)
5. Write a R program for calculating cumulative sums, and products minima maxima and calculus *.
cumsum(1:4)
cumsum(-1:-6)
cumprod(1:4)
cumprod(-1:-6)
data = c(23, 4, 56, 21, 34, 56, 73)
print(min(data))
print(max(data))
data = data.frame(c1 = c(23, 4, 56, 21), c2 = c("Shel", "Mic", "Var", "Rag"), c3 = c(1.3, 4.6, 7.8, 6.3))
data
min(data$c1)
min(data$c2)
min(data$c3)
max(data$c1)
max(data$c2)
max(data$c3)
f = expression(6*x^3+10*x^2+2)
D(f, "x")
f = function(x){
return (6*x^3+10*x^2+2)
}
integrate(f, 0, 5)
6. Write a R program for finding stationary distribution of markanovchains *.
install.packages("markovchain")
install.packages("diagram")
trans_mat = matrix(c(0.7,0.3,0.1,0.9),nrow=2,byrow = TRUE)
trans_mat
library(markovchain)
charState <- c("Pepsi", "Coke")
disc_trans <- new("markovchain", transitionMatrix = trans_mat, states = charState)
disc_trans
steadyStates(disc_trans)
plot(disc_trans)
7. Write a R program that include linear algebra operations on vectors and matrices *.
d1 = scan()
4
2
1
6
4
d2 = scan()
9
1
-1
8
2
d2
t(d1)
7*d1
d1+d2
sum(d1*d2)
sqrt(sum(d1*d2))
v1 <- c(1, 1)
v2 <- c(-1, 1)
sum(v1 * v2)
A <- matrix(c(1, 3, 2, 2, 8, 9), ncol = 3)
A
7 *A
t(A)
B <- matrix(c(5, 8, 3, 4, 2, 7), ncol = 3, byrow = T)
B
A+B
A <- matrix(c(1, 3, 2, 2, 8, 9), ncol = 2)
B <- matrix(c(5, 8, 4, 2), ncol = 2)
A %*% B
matrix(0, nrow = 2, ncol = 3)
matrix(1, nrow = 2, ncol = 3)
diag(c(1, 2, 3))
8. Write a R program for any visual representation of an object with creating graphs using graphic functions: Plot(), Hist(), Linechart(), Pie() ,Boxplot(), Scatterplots() *.
x = 1:5
y = x*x
plot(x, y, type = "l")
plot(x, y, type = "h")
v = c(19, 23, 11, 5, 16)
hist(v, xlab = "No of Articles", col = "green", border = "black", xlim = c(0, 50), ylim = c(0, 5), breaks = 5)
v1 = c(7, 12, 28, 3, 41)
t = c(14, 7, 6, 19, 3)
plot(v1, type = "o", col = "red", xlab = "month", ylab = "Rain fall", main = "Rain fall chart")
lines(t, type = "o", col = "blue")
labels= c("Mumbai", "Pune", "Chennai", "Bangalore")
piepercent = round(100 * 4 / sum(v1), 1)
pie(v1, labels = labels, main = "City pie chart", col = rainbow(length(v1)))
legend("topright", labels, cex = 0.5, fill = rainbow(length(v1)))
data(mtcars)
boxplot(disp~gear, data = mtcars, main = "Displacement by Gear", xlab = "Gear", ylab = "Displacement")
input = mtcars[,c('wt', 'mpg')]
plot(x = input$wt, y = input$mpg, xlab = "weight", ylab = "Milage", xlim = c(1.5, 4), ylim = c(10, 25), main = "Weight vs Milage")
9. Write a R program for with any data set containing dataframe objects, indexing and subsetting data frames, and employ manipulating and analyzing data *.
friend.data = data.frame(f_id = c(1:5),
F_name = c("Shel", "Mic", "Var", "Ragh", "Pav"),
F_location = c("Dha", "Mumbai", "Pune", "Hubli", "Thai"),
stringAsFactors = FALSE)
print(friend.data)
print(summary(friend.data))
result = data.frame(friend.data$F_name)
result
data = subset(friend.data, f_id != 3)
data
library(dplyr)
data = select(friend.data, -F_location)
data
print(friend.data[1:2, ])
10. Write a program to create an any application of Linear Regression in multivariate context for predictive purpose *.
custom_data = data.frame(
mpg = c(21, 18, 23, 25, 19),
wt = c(2.8, 3.2, 2.5, 2.1, 2.9),
hp = c(110, 130, 95, 120, 105),
qsec = c(15.5, 16.2, 14.8, 14.5, 16.0)
)
custom_model = lm(mpg~wt+hp+qsec,data = custom_data)
summary(custom_model)
custom_predictions = predict(custom_model, newdata = custom_data)
cat("Predicted MPG values: \n", custom_predictions)