R Programming Solvw
R Programming Solvw
R Programming Solvw
# Addition
sum_ab <- a + b
cat("Addition:", sum_ab, "\n")
# Subtraction
diff_ab <- a - b
cat("Subtraction:", diff_ab, "\n")
# Multiplication
prod_ab <- a * b
cat("Multiplication:", prod_ab, "\n")
# Division
div_ab <- a / b
cat("Division:", div_ab, "\n")
# Exponentiation
exp_ab <- a ^ b
cat("Exponentiation:", exp_ab, "\n")
# Square Root
sqrt_a <- sqrt(a)
cat("Square Root of a:", sqrt_a, "\n")
# Trigonometric Functions
angle <- 45 # in degrees
# Sine
sin_val <- sin(angle * (pi / 180))
cat("Sine:", sin_val, "\n")
# Cosine
cos_val <- cos(angle * (pi / 180))
cat("Cosine:", cos_val, "\n")
# Tangent
tan_val <- tan(angle * (pi / 180))
cat("Tangent:", tan_val, "\n")
# Logarithmic Functions
x <- 100
# Natural Logarithm
ln_x <- log(x)
cat("Natural Logarithm of x:", ln_x, "\n")
OUTPUT:
# 2. Convert to uppercase
uppercase_string <- toupper(my_string)
cat("2. Uppercase: ", uppercase_string, "\n")
# 3. Convert to lowercase
lowercase_string <- tolower(my_string)
cat("3. Lowercase: ", lowercase_string, "\n")
# 4. Substring
substring <- substr(my_string, start = 1, stop = 5)
cat("4. Substring (1-5): ", substring, "\n")
# 7. Replace a substring
replaced_string <- gsub("World", "R", my_string)
cat("7. Replaced: ", replaced_string, "\n")
OUTPUT:
# Matrix Addition
matrix_sum <- matrix_A + matrix_B
print("Matrix Addition Result:")
print(matrix_sum)
# Matrix Subtraction
matrix_diff <- matrix_A - matrix_B
print("Matrix Subtraction Result:")
print(matrix_diff)
# Matrix Multiplication
matrix_product <- matrix_A * matrix_B
print("Matrix Multiplication Result:")
print(matrix_product)
# Transpose of Matrix A
matrix_A_transpose <- t(matrix_A)
print("Transpose of Matrix A:")
print(matrix_A_transpose)
# Determinant of Matrix A
determinant_A <- det(matrix_A)
print(paste("Determinant of Matrix A:", determinant_A))
} else {
print("Matrix A is not square and determinant cannot be computed.")
}
# Find the product of Transpose of matrix "tmp" and original matrix "tmp"
tmp_product <- tmp_transpose %*% tmp
print("Product of Transpose of Matrix tmp and Matrix tmp:")
print(tmp_product)
# Print the matrix with the values of column and row numbers
matE <- abs(col(matE) - row(matE))
print("Matrix with values of column and row numbers:")
print(matE)
OUTPUT:
sudoku[1, 1] <- 3
sudoku[2, 2] <- 2
sudoku[3, 3] <- 1
print(sudoku)
OUTPUT:
>
OUTPUT:
OUTPUT:
# Given dataframe
exam_data <- data.frame(
name = c('Anastasia', 'Dima', 'Katherine', 'James', 'Emily', 'Michael', 'Matthew', 'Laura',
'Kevin', 'Jonas'),
score = c(12.5, 9, 16.5, 12, 9, 20, 14.5, 13.5, 8, 19),
attempts = c(1, 3, 2, 3, 2, 3, 1, 1, 2, 1),
qualify = c('yes', 'no', 'yes', 'no', 'no', 'yes', 'yes', 'no', 'no', 'yes')
)
OUTPUT:
# Given dataframe
> exam_data <- data.frame(
+ name = c('Anastasia', 'Dima', 'Katherine', 'James', 'Emily',
'Michael', 'Matthew', 'Laura', 'Kevin', 'Jonas'),
+ score = c(12.5, 9, 16.5, 12, 9, 20, 14.5, 13.5, 8, 19),
+ attempts = c(1, 3, 2, 3, 2, 3, 1, 1, 2, 1),
+ qualify = c('yes', 'no', 'yes', 'no', 'no', 'yes', 'yes', 'no', 'no',
'yes')
+ )
>
> # Print original dataframe and its structure
> print("Original dataframe:")
[1] "Original dataframe:"
> print(exam_data)
name score attempts qualify
1 Anastasia 12.5 1 yes
2 Dima 9.0 3 no
3 Katherine 16.5 2 yes
4 James 12.0 3 no
5 Emily 9.0 2 no
6 Michael 20.0 3 yes
7 Matthew 14.5 1 yes
8 Laura 13.5 1 no
9 Kevin 8.0 2 no
10 Jonas 19.0 1 yes
> print("Structure of the said data frame:")
[1] "Structure of the said data frame:"
> print(str(exam_data))
'data.frame': 10 obs. of 4 variables:
$ name : chr "Anastasia" "Dima" "Katherine" "James" ...
$ score : num 12.5 9 16.5 12 9 20 14.5 13.5 8 19
$ attempts: num 1 3 2 3 2 3 1 1 2 1
$ qualify : chr "yes" "no" "yes" "no" ...
NULL
# Given dataframe
exam_data <- data.frame(
name = c('Anastasia', 'Dima', 'Katherine', 'James', 'Emily', 'Michael', 'Matthew', 'Laura',
'Kevin', 'Jonas'),
score = c(12.5, 9, 16.5, 12, 9, 20, 14.5, 13.5, 8, 19),
attempts = c(1, 3, 2, 3, 2, 3, 1, 1, 2, 1),
qualify = c('yes', 'no', 'yes', 'no', 'no', 'yes', 'yes', 'no', 'no', 'yes')
)
OUTPUT:
# Given dataframe
> exam_data <- data.frame(
+ name = c('Anastasia', 'Dima', 'Katherine', 'James', 'Emily',
'Michael', 'Matthew', 'Laura', 'Kevin', 'Jonas'),
+ score = c(12.5, 9, 16.5, 12, 9, 20, 14.5, 13.5, 8, 19),
+ attempts = c(1, 3, 2, 3, 2, 3, 1, 1, 2, 1),
+ qualify = c('yes', 'no', 'yes', 'no', 'no', 'yes', 'yes', 'no', 'no',
'yes')
+ )
>
> # Print original dataframe
> print("Original dataframe:")
[1] "Original dataframe:"
> print(exam_data)
name score attempts qualify
1 Anastasia 12.5 1 yes
2 Dima 9.0 3 no
3 Katherine 16.5 2 yes
4 James 12.0 3 no
5 Emily 9.0 2 no
6 Michael 20.0 3 yes
7 Matthew 14.5 1 yes
8 Laura 13.5 1 no
9 Kevin 8.0 2 no
10 Jonas 19.0 1 yes
>
> # Reorder dataframe by column name
> exam_data <- exam_data[c("name", "attempts", "score", "qualify")]
> print("Reorder by column name:")
[1] "Reorder by column name:"
> print(exam_data)
name attempts score qualify
1 Anastasia 1 12.5 yes
2 Dima 3 9.0 no
3 Katherine 2 16.5 yes
4 James 3 12.0 no
5 Emily 2 9.0 no
6 Michael 3 20.0 yes
7 Matthew 1 14.5 yes
8 Laura 1 13.5 no
9 Kevin 2 8.0 no
10 Jonas 1 19.0 yes
print("Original Dataframes:")
print(a)
print(b)
OUTPUT:
# Given dataframes
> a <- c("a", "b", "c", "d", "e")
> b <- c("d", "e", "f", "g")
>
> print("Original Dataframes:")
[1] "Original Dataframes:"
> print(a)
[1] "a" "b" "c" "d" "e"
> print(b)
[1] "d" "e" "f" "g"
>
> # Find elements present in both dataframes
> result <- intersect(a, b)
> print("Elements which are present in both dataframes:")
[1] "Elements which are present in both dataframes:"
> print(result)
[1] "d" "e"
# Order the entire data frame by the first and second column
result <- data[order(data[,1], data[,2]),]
print("Order the entire data frame by the first and second column:")
print(result)
OUTPUT:
>
7. Find the 25th quantile of a binomial distribution with 25
trials and probability of success on each trial = 0.5
print(quantile_25)
print(quantile_complement)
random_values <- c(8, 14, 10, 12, 10, 14, 16, 7, 13, 12)
print(random_values)
OUTPUT :
OUTPUT :
> #find the probability of 10 successes during 12 trials where the
probability of
> #success on each trial is 0.6
> dbinom(x=10, size=12, prob=.6)
[1] 0.06385228
8.B. Subha flips a fair coin 20 times. What is the probability that the
coin lands on heads exactly 7 times?
OUTPUT :
> dbinom(3, size=5, prob=0.65)
[1] 0.3364156
OUTPUT:
> pbinom(4, size=5, prob=.2)
[1] 0.99968
rbinom(100,20,0.5)
OUTPUT:
> rbinom(100,20,0.5)
[1] 14 14 7 10 9 8 10 12 11 11 12 9 8 14 9 9 7 10 10 8 8
8 7
[24] 11 13 10 10 7 11 10 8 10 11 4 10 7 11 10 12 7 7 9 9 10
13 13
[47] 8 8 12 6 11 11 10 7 6 13 9 9 10 10 13 11 8 10 9 10 9
6 13
[70] 12 7 6 14 9 12 8 9 12 12 8 6 8 10 12 10 10 13 9 10 12
5 4
[93] 8 10 13 14 8 10 8 11
9. Write a function in R that randomly draws five cards from the deck of card
face <- c("king", "queen", "jack", "ten", "nine", "eight", "seven", "six", "five", "four", "three", "two",
"ace")
num = rep(num, 4)
deck[sample(nrow(deck), n), ]
print(result)
OUTPUT :
> suit <- c("clubs", "diamonds", "hearts", "spades")
> face <- c("king", "queen", "jack", "ten", "nine", "eight", "seven",
"six", "five", "four", "three", "two", "ace")
> num <- c(13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1)
> deck <- data.frame(
+ face = rep(face, 4),
+ suit = c(rep("spades", 13), rep("clubs", 13), rep("diamonds", 13),
rep("hearts", 13)),
+ num = rep(num, 4)
+ )
>
> draw_n_random_cards <- function(deck, n) {
+ deck[sample(nrow(deck), n), ]
+ }
>
> result <- draw_n_random_cards(deck, 5)
> print(result)
face suit num
28 queen diamonds 12
51 two hearts 2
34 six diamonds 6
7 seven spades 7
4 ten spades 10
10.A. Find the value of the standard normal distribution pdf at x=0
dnorm(x=0, mean=0, sd=1)
# [1] 0.3989423
# by default, R uses mean=0 and sd=1
dnorm(x=0)
# [1] 0.3989423
#find the value of the normal distribution pdf at x=10 with
mean=20 and sd=5
dnorm(x=10, mean=20, sd=5)
OUTPUT:
> dnorm(x=0, mean=0, sd=1)
[1] 0.3989423
> # [1] 0.3989423
> # by default, R uses mean=0 and sd=1
> dnorm(x=0)
[1] 0.3989423
> # [1] 0.3989423
> #find the value of the normal distribution pdf at x=10 with
mean=20 and sd=5
> dnorm(x=10, mean=20, sd=5)
[1] 0.01079819
10.B. Suppose the height of males at a certain school is normally distributed with a mean
of μ=70 inches and a standard deviation of σ = 2 inches. Approximately what percentage
of males at this school are taller than 74 inches?
#find the percentage of males that are taller than 74 inches in a population with
#mean = 70 and sd = 2
pnorm(74, mean=70,
sd=2, lower.tail=FALSE)
OUTPUT:
> #find the percentage of males that are taller than 74 inches in a
population with
> #mean = 70 and sd = 2
> pnorm(74, mean=70,
+ sd=2, lower.tail=FALSE)
[1] 0.02275013
10.C. Suppose the weight of a certain species of otters is normally distributed with a
mean of μ=30 lbs and a standard deviation of σ = 5 lbs. Approximately what percentage
of this species of otters weight less than 22 lbs?
#find percentage of otters that weight less than 22 lbs in a population with
#mean = 30 and sd = 5
pnorm(22, mean=30, sd=5)
OUTPUT:
> #find percentage of otters that weight less than 22 lbs in a
population with
> #mean = 30 and sd = 5
> pnorm(22, mean=30, sd=5)
[1] 0.05479929
10.D. Suppose the height of plants in a certain region is normally distributed with
a mean of μ=13 inches and a standard deviation of σ = 2 inches. Approximately
what percentage of plants in this region are between 10 and 14 inches tall?
#find percentage of plants that are less than 14 inches tall, then subtract the
#percentage of plants that are less than 10 inches tall, based on a population
#with mean = 13 and sd = 2
pnorm(14, mean=13, sd=2) - pnorm(10, mean=13, sd=2)
OUTPUT :
> #find percentage of plants that are less than 14 inches tall, then
subtract the
> #percentage of plants that are less than 10 inches tall, based on
a population
> #with mean = 13 and sd = 2
> pnorm(14, mean=13, sd=2) - pnorm(10, mean=13, sd=2)
[1] 0.6246553
10. E.Suppose that you have a machine that packages rice inside boxes. The
process follows a Normal distribution and it is known that the mean of the
weight of each box is 1000 grams and the standard deviation is 10 grams.
What is the probability of a box weighing exactly 950 grams?
pnorm(980,1000,10,lower.tail=FALSE)
#Calculate the quantile for probability 0.5 for the above scenario.
qnorm(0.5,1000,10)
rnorm(10,1000,10)
OUTPUT:
> dnorm( 950, 1000,10)
[1] 1.48672e-07
> #What is the probability of a box weighing more than 980 grams?
> pnorm(980,1000,10,lower.tail=FALSE)
[1] 0.9772499
> #Calculate the quantile for probability 0.5 for the above scenario.
> qnorm(0.5,1000,10)
[1] 1000
> #Simulate the above scenario for 10 observations.
> rnorm(10,1000,10)
[1] 1011.0971 998.3551 1006.1722 997.9489 1020.7643 1006.1655 1002.1544
[8] 1000.9453 998.5481 991.4324