R Programming Shiv
R Programming Shiv
(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
Page | 1
Name :- Shivendra Sony Roll No :- 11222768 Class/Group :- B.Tech.(CSE)-5th Sem / F1
Page | 2
Name :- Shivendra Sony Roll No :- 11222768 Class/Group :- B.Tech.(CSE)-5th Sem / F1
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.
Page | 4
Name :- Shivendra Sony Roll No :- 11222768 Class/Group :- B.Tech.(CSE)-5th Sem / F1
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.
Page | 6
Name :- Shivendra Sony Roll No :- 11222768 Class/Group :- B.Tech.(CSE)-5th Sem / F1
Page | 7
Name :- Shivendra Sony Roll No :- 11222768 Class/Group :- B.Tech.(CSE)-5th Sem / F1
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")
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")
o Membership Operator
➢ 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
return(sum)
}
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)
}
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 :-
➢ 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 :-
➢ Output :-
Page | 17
Name :- Shivendra Sony Roll No :- 11222768 Class/Group :- B.Tech.(CSE)-5th Sem / F1
Page | 18