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

R Programming Shiv

Uploaded by

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

R Programming Shiv

Uploaded by

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

Name :- Shivendra Sony Roll No :- 11222768 Class/Group :- B.Tech.

(CSE)-5th Sem / F1

Experiment :- 01
Aim :- To install of R and R-Studio in the system.

1. To install R, go to cran.r-project.org

2. Click Download R for Windows.

Page | 1
Name :- Shivendra Sony Roll No :- 11222768 Class/Group :- B.Tech.(CSE)-5th Sem / F1

3. Install R Click on install R for the first time.

Page | 2
Name :- Shivendra Sony Roll No :- 11222768 Class/Group :- B.Tech.(CSE)-5th Sem / F1

4. Click Download R for Windows. Open the downloaded file.

5. Select the language you would like to use during the installation.
Then click OK.

Page | 3
Name :- Shivendra Sony Roll No :- 11222768 Class/Group :- B.Tech.(CSE)-5th Sem / F1

6. Click Next.

7. Select where you would like R to be installed. It will default to your


Program Files on your C Drive. Click Next.

8. You can then choose which installation you would like.

Page | 4
Name :- Shivendra Sony Roll No :- 11222768 Class/Group :- B.Tech.(CSE)-5th Sem / F1

9. (Optional) If your computer is a 64-bit, you can choose the 64-


bit User Installation. Then click Next.

10. Then specify if you want to customized your startup or just use
the defaults. Then click Next.

Page | 5
Name :- Shivendra Sony Roll No :- 11222768 Class/Group :- B.Tech.(CSE)-5th Sem / F1

11. Then you can choose the folder that you want R to be saved
within or the default if the R folder that was created. Once you
have finished, click Next.

12. You can then select additional shortcuts if you would like. Click
Next.

13. Click Finish.

Page | 6
Name :- Shivendra Sony Roll No :- 11222768 Class/Group :- B.Tech.(CSE)-5th Sem / F1

14. Next, download RStudio. Go to https://posit.co/downloads/

15. Click Download RStudio.

Page | 7
Name :- Shivendra Sony Roll No :- 11222768 Class/Group :- B.Tech.(CSE)-5th Sem / F1

16. Once the packet has downloaded, the Welcome to RStudio


Setup Wizard will open. Click Next and go through the
installation steps.

17. After the Setup Wizard finishing the installation, RStudio will
open.

Page | 8
Name :- Shivendra Sony Roll No :- 11222768 Class/Group :- B.Tech.(CSE)-5th Sem / F1

Experiment :- 02
Aim :- Write a program to implement operators in R programming .

Arithmetic Operators :-
a <- 10 b
<- 5
add <- a + b # Addition sub <-
a-b # Subtraction mul <- a *
b # Multiplication div <- a / b
# Division mod <- a %% b #
Modulus exp <- a ^ b #
Exponentiation cat("Addition: ", add, "\n")
cat("Subtraction: ", sub, "\n") cat("Multiplication: ", mul,
"\n") cat("Division: ", div, "\n") cat("Modulus: ", mod, "\n")
cat("Exponentiation: ", exp, "\n")

Relational Operators :-
x <- 10 y <- 20 cat("x is equal to y:", x == y, "\n")
cat("x is not equal to y:", x != y, "\n") cat("x is
greater than y:", x > y, "\n") cat("x is less than
y:", x < y, "\n") cat("x is greater than or equal to
y:", x >= y, "\n") cat("x is less than or equal to
y:", x <= y, "\n")

• Logical Operators :- p <- TRUE q <- FALSE


cat("p AND q:", p & q, "\n") # Logical AND cat("p
OR q:", p | q, "\n") # Logical OR cat("NOT p:",
!p, "\n") # Logical NOT

Page | 9
Name :- Shivendra Sony Roll No :- 11222768 Class/Group :- B.Tech.(CSE)-5th Sem / F1

• Assignment Operators :-
x <- 5 # Leftward assignment y = 7
# Alternative leftward assignment z <- x + y cat("Value of z:", z, "\n")
10 -> a # Rightward assignment
cat("Value of a:", a, "\n")

Miscellaneous Operators :- o Sequence


Operator seq_vec <- 1:10 # Generates a sequence
from 1 to 10 cat("Sequence:", seq_vec, "\n")

o Membership Operator

v <- c(1, 2, 3, 4, 5) cat("Is 3 in the


vector?:", 3 %in% v, "\n")

o Matrix multiplication operator m1 <-


matrix(c(1, 2, 3, 4), nrow = 2, ncol = 2) m2 <-
matrix(c(5, 6, 7, 8), nrow = 2, ncol = 2) result
<- m1 %*% m2
cat("Matrix Multiplication Result:\n") print(result)

➢ Output :-

Page | 10
Name :- Shivendra Sony Roll No :- 11222768 Class/Group :- B.Tech.(CSE)-5th Sem / F1

Page | 11
Name :- Shivendra Sony Roll No :- 11222768 Class/Group :- B.Tech.(CSE)-5th Sem / F1

Experiment :- 03
AIM:- Write a program to check whether a year (integer) entered by
the user is a leap year or not?
➢ Code :-
is_leap_year <- function(year) { if ((year %% 4 == 0 && year %%
100 != 0) || (year %% 400 == 0)) { return(TRUE) } else {
return(FALSE)
} } year <- as.integer(readline(prompt = "Enter a
year: ")) if (is_leap_year(year)) { cat(year, "is a leap
year.\n")
} else { cat(year, "is not a leap
year.\n")
}
➢ Output :-

Page | 12
Name :- Shivendra Sony Roll No :- 11222768 Class/Group :- B.Tech.(CSE)-5th Sem / F1

Experiment :- 04
AIM:- Write an R program to find the sum of natural numbers without
formula using the if–else statement and the while loop.
➢ Code :- sum_natural_numbers <- function(n) {
sum <- 0
counter <- 1

while (counter <= n) {


if (counter <= n) {
sum <- sum + counter
}
counter <- counter + 1
}

return(sum)
}

n <- as.integer(readline(prompt="Enter a positive integer: "))

if (n > 0) {
result <- sum_natural_numbers(n)
cat("The sum of the first", n, "natural numbers is:", result, "\n")
} else {
cat("Enter a positive integer.\n")
}
➢ Output :-

Page | 13
Name :- Shivendra Sony Roll No :- 11222768 Class/Group :- B.Tech.(CSE)-5th Sem / F1

Experiment :- 05
AIM:- Write an R program to make a simple calculator that can add,
subtract, multiply and divide using switch cases and functions.
➢ Code :-
add <- function(a, b) {
return(a + b)
}

subtract <- function(a, b) {


return(a - b)
}

multiply <- function(a, b) {


return(a * b)
}

divide <- function(a, b) {


if (b != 0) {
return(a / b) }
else {
return("Error: Division by zero")
}
}

simple_calculator <- function() {

a <- as.numeric(readline(prompt = "Enter the first number: "))

b <- as.numeric(readline(prompt = "Enter the second number: "))

operator <- readline(prompt = "Enter the operator (+, -, *, /): ")

result <- switch(operator,


"+" = add(a, b),
"-" = subtract(a, b),
"*" = multiply(a, b),
"/" = divide(a, b),
"Invalid operator")

cat("The result is:", result, "\n")

Page | 14
Name :- Shivendra Sony Roll No :- 11222768 Class/Group :- B.Tech.(CSE)-5th Sem / F1

simple_calculator()

➢ Output :-

Page | 15
Name :- Shivendra Sony Roll No :- 11222768 Class/Group :- B.Tech.(CSE)-5th Sem / F1

Experiment :- 06
Aim :- Write a program to perform searching within a list (1 to 50). If
the number is found in the list, print that the search is successful
otherwise print that the number is not in the list.
➢ Code :-

search_number <- function(number) {

numbers_list <- 1:50

if (number %in% numbers_list) {


print("Search successful")
} else {
print("Number not in the list")
}
}

number_to_search <- as.numeric(readline(prompt="Enter the number to search: "))


search_number(number_to_search)

➢ Output :-

Page | 16
Name :- Shivendra Sony Roll No :- 11222768 Class/Group :- B.Tech.(CSE)-5th Sem / F1

Experiment :- 07
Aim :- Create a list and data frame that stores the marks of any three
subjects for 10 students. Find out the total marks, average,
maximum marks and minimum marks of every subject.

➢ Code :-

student_marks_list <- list(


Subject1 = c(75, 85, 90, 60, 70, 88, 76, 82, 94, 67),
Subject2 = c(65, 77, 89, 92, 81, 75, 68, 84, 91, 70),
Subject3 = c(88, 90, 77, 85, 93, 66, 78, 92, 81, 74)
)

student_marks_df <- data.frame(student_marks_list)

print("Marks of Students:") print(student_marks_df)

total_marks <- colSums(student_marks_df) print("Total


marks for each subject:") print(total_marks)

average_marks <- colMeans(student_marks_df)


print("Average marks for each subject:")
print(average_marks)

max_marks <- apply(student_marks_df, 2, max)


print("Maximum marks for each subject:")
print(max_marks)

min_marks <- apply(student_marks_df, 2, min)


print("Minimum marks for each subject:")
print(min_marks)

➢ Output :-

Page | 17
Name :- Shivendra Sony Roll No :- 11222768 Class/Group :- B.Tech.(CSE)-5th Sem / F1

Page | 18

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy