0% found this document useful (0 votes)
10 views18 pages

R Programming Data Analysis Examples

The document outlines various R programming exercises including temperature conversion, area calculations for geometric shapes, finding even numbers, and string manipulation. It also covers data structures, statistical analysis, recursive functions, and counting even and odd numbers in an array. Each section provides an aim, algorithm, and sample program code for implementation.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views18 pages

R Programming Data Analysis Examples

The document outlines various R programming exercises including temperature conversion, area calculations for geometric shapes, finding even numbers, and string manipulation. It also covers data structures, statistical analysis, recursive functions, and counting even and odd numbers in an array. Each section provides an aim, algorithm, and sample program code for implementation.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Data analytics using R programming

lab

[Link] to convert the given temperature from Fahrenheit to Celsius


and vice versa depending upon user’s choice
Temperature conversion (F ↔ C)
Aim: Convert temperature between Fahrenheit and Celsius based on user choice.
Algorithm:
1. Ask user to choose 1 for Celsius to Fahrenheit, 2 for Fahrenheit to Celsius.
2. Read the choice.

3. If choice is 1, read Celsius and compute F=C × 9/5+32.

4. If choice is 2, read Fahrenheit and compute C=(F−32)×5/9 .

5. Print the result.


Program:
cat("1. Celsius to Fahrenheit\n")
cat("2. Fahrenheit to Celsius\n")
choice <- [Link](readline("Enter your choice (1/2): "))
if (choice == 1) {
c <- [Link](readline("Enter temperature in Celsius: "))
f <- (c * 9/5) + 32
cat("Temperature in Fahrenheit =", f, "\n")
} else if (choice == 2) {
f <- [Link](readline("Enter temperature in Fahrenheit: "))
c <- (f - 32) * 5/9
cat("Temperature in Celsius =", c, "\n")
} else {
cat("Invalid choice\n")
}
Output:
2. Program, to find the area of rectangle, square, circle and triangle by
accepting suitable input parameters from user.
Areas: rectangle, square, circle, triangle
Aim: Find area of rectangle, square, circle and triangle from user input.
Algorithm:
1. Show menu (1–rectangle, 2–square, 3–circle, 4–triangle).
2. Read user choice.
3. For each choice, read required dimensions and compute area.
4. Print the area.
program
cat("1. Rectangle\n2. Square\n3. Circle\n4. Triangle\n")
ch <- [Link](readline("Enter your choice: "))
if (ch == 1) {
l <- [Link](readline("Enter length: "))
b <- [Link](readline("Enter breadth: "))
area <- l * b
cat("Area of rectangle =", area, "\n")
} else if (ch == 2) {
s <- [Link](readline("Enter side: "))
area <- s * s
cat("Area of square =", area, "\n")
} else if (ch == 3) {
r <- [Link](readline("Enter radius: "))
area <- pi * r * r
cat("Area of circle =", area, "\n")
} else if (ch == 4) {
b <- [Link](readline("Enter base: "))
h <- [Link](readline("Enter height: "))
area <- 0.5 * b * h
cat("Area of triangle =", area, "\n")
} else {
cat("Invalid choice\n")
}
Output:

3. Write a program to find list of even numbers from 1 to n using RLoops.


Even numbers from 1 to n (Loops)
Aim: Print all even numbers from 1 to n using a loop.
Algorithm:
1. Read n.
2. Use for-loop from 1 to n.
3. If number is divisible by 2, print it.
sProgram:
n <- [Link](readline("Enter n: "))
cat("Even numbers from 1 to", n, "are:\n")
for (i in 1:n) {
if (i %% 2 == 0) {
cat(i, " ")
}
}
cat("\n")
output:

4. Create a function to print squares of numbers in sequence.


Function to print squares in sequence
Aim: Create a function to print squares of numbers from 1 to n.
Algorithm:
1. Define a function printSquares(n).
2. Inside, loop from 1 to n.
3. For each i, compute i 2 and print.
4. Call the function.
Program:
printSquares <- function(n) {
for (i in 1:n) {
sq <- i * i
cat("Square of", i, "is", sq, "\n")
}
}

num <- [Link](readline("Enter n: "))


printSquares(num)

output:

5. Write a program to join columns and rows in a data frame using cbind() and
rbind() in R.
Join columns and rows using cbind() and rbind()
Aim: Join data frame columns and rows using cbind() and rbind().
Algorithm:
1. Create two simple data frames with same number of rows.
2. Use cbind() to join them side by side.
3. Create two data frames with same columns.
4. Use rbind() to join them one below another.
Program:
# Using cbind()
id <- c(1, 2, 3)
name <- c("A", "B", "C")
age <- c(20, 21, 22)
df1 <- [Link](id, name)
df2 <- [Link](age)
df_col_join <- cbind(df1, df2)
print("After cbind():")
print(df_col_join)
# Using rbind()
df3 <- [Link](id = 4, name = "D", age = 23)
df4 <- [Link](id = 5, name = "E", age = 24)
df_row_join <- rbind(df_col_join, df3, df4)
print("After rbind():")
print(df_row_join)

Output:
6. Implement different String Manipulation functions in R.
String manipulation functions in R
Aim: Show basic string manipulation operations.
Algorithm:
1. Take example strings.
2. Apply functions: nchar, toupper, tolower, substr, paste.
3. Print results.

program:
str1 <- "hello"
str2 <- "world"
# length of string
cat("nchar(str1) =", nchar(str1), "\n")
# upper and lower
cat("toupper(str1) =", toupper(str1), "\n")
cat("tolower(str2) =", tolower(str2), "\n")
# substring
cat("substr(str1, 2, 4) =", substr(str1, 2, 4), "\n")
# concatenation
str3 <- paste(str1, str2) # "hello world"
cat("paste(str1, str2) =", str3, "\n")
# paste with separator
str4 <- paste(str1, str2, sep = "-")
cat("paste(str1, str2, sep='-') =", str4, "\n")

output:
7. Implement different data structures in R (Vectors, Lists, Data Frames)
Basic data structures: Vectors, Lists, Data Frames
Aim: Create and display basic R data structures.
Algorithm:
1. Create a numeric vector.
2. Create a list with mixed types.
3. Create a data frame from vectors.
4. Print all of them.
Program:
# Vector
v <- c(10, 20, 30, 40)
cat("Vector v:\n")
print(v)
# List
my_list <- list(name = "Ram", age = 21, marks = c(80, 85, 90))
cat("List my_list:\n")
print(my_list)
# Data frame
id <- c(1, 2, 3)
name <- c("A", "B", "C")
marks <- c(80, 75, 90)
students <- [Link](id, name, marks)
cat("Data frame students:\n")
print(students)

output:
9 Create pie chart and bar chart using R.
Pie chart and bar chart
Aim: Create pie chart and bar chart from simple data.
Algorithm:
1. Create a numeric vector of values and labels.
2. Call pie() with values and labels.
3. For bar chart, use barplot() with same data
Program:
values <- c(100, 50, 30, 40)
labels <- c("A", "B", "C", "D")
# Pie chart
pie(values, labels = labels, main = "Simple Pie Chart") # [web:2][web:5]
# Bar chart
barplot(values, [Link] = labels, main = "Simple Bar Chart",
xlab = "Category", ylab = "Value", col = "red")
output:

10. Create a data set and do statistical analysis on the data using R.
Create dataset and do statistical analysis
Aim: Create a numeric dataset and compute basic statistics.
Algorithm:
1. Create a numeric vector (marks, heights, etc.).
2. Compute mean, median, variance, standard deviation, min, max.
3. Print the results.

program:
marks <- c(50, 60, 70, 80, 90, 75, 65, 85)
cat("Data:\n")
print(marks)
cat("Mean =", mean(marks), "\n")
cat("Median =", median(marks), "\n")
cat("Variance =", var(marks), "\n")
cat("Standard Deviation =", sd(marks), "\n")
cat("Minimum =", min(marks), "\n")
cat("Maximum =", max(marks), "\n")
output:

11 Program to find factorial of the given number using recursive function


Factorial using recursive function
Aim: Find factorial of a number using recursion.
Algorithm:
1. Define function fact(n).

2. If n=¿ 0 or n=¿ 1, return 1.

3. Else return n∗(n−1)!.

4. Read n, call fact(n) and print result.

program:
fact <- function(n) {
if (n == 0 || n == 1) {
return(1)
} else {
return(n * fact(n - 1))
}
}
num <- [Link](readline("Enter a number: "))
result <- fact(num)
cat("Factorial of", num, "is", result, "\n")

output:
12 Write a R program to count the number of even and odd numbers from array
of N numbers.
Count even and odd numbers in an array
Aim: Count how many even and odd numbers are in an array of N numbers.
Algorithm:
1. Read N.
2. Read N numbers into a vector.
3. Set even_count = 0, odd_count = 0.
4. Loop through vector: if element %% 2 == 0, increase even_count else
odd_count.
5. Print both counts.

program:
N <- [Link](readline("Enter number of elements N: "))
arr <- numeric(N)
for (i in 1:N) {
arr[i] <- [Link](readline(paste("Enter element", i, ": ")))
}
even_count <- 0
odd_count <- 0
for (x in arr) {
if (x %% 2 == 0) {
even_count <- even_count + 1
} else {
odd_count <- odd_count + 1
}
}

cat("Number of even elements:", even_count, "\n")


cat("Number of odd elements:", odd_count, "\n")

output:

You might also like