R Programs 2024-2025
R Programs 2024-2025
Program 2: Write a R program to find the maximum and the minimum value of a given
vector.
x = c(10, 20, 30, 25, 9, 26)
print("Original Vectors:")
print(x)
print("Maximum value of the above Vector:")
print(max(x))
print("Minimum value of the above Vector:")
print(min(x))
Output:
Program 3: Write a R program to get all prime numbers up to a given number.
N <- 25
# Function to check if a number is prime
is_prime <- function(num) {
if (num <= 1) {
return(FALSE)
}
for (i in 2:sqrt(num)) {
if (num %% i == 0) {
return(FALSE)
}
}
return(TRUE)
}
# Generate and print prime numbers up to N
cat("Prime numbers up to", N, "are:\n")
for (i in 2:N) {
if (is_prime(i)) {
cat(i, " ")
}
}
Output:
3 5 7 11 13 17 19 23
Program 4: Write a R program to get the unique elements of a given string and unique
numbers of vector
str1 = "The quick brown fox jumps over the lazy dog."
print("Original vector(string)")
print(str1)
print("Unique elements of the said vector:")
print(unique(tolower(str1)))
nums = c(1, 2, 2, 3, 4, 4, 5, 6)
print("Original vector(number)")
print(nums)
print("Unique elements of the said vector:")
print(unique(nums))
Output:
Program 5: Two Categorical Variables – Discover relationships within a dataset
data(mtcars)
head(mtcars)
install.packages("ggplot2")
library(ggplot2)
"cyl" and "gear"
age_vs_employment <-margin.table(cross_tab,c(2,3))
print(age_vs_employment)
Output:
Program 7:
install.packages(c("ggplot2", "ggpubr", "tidyverse", "broom",
"AICcmodavg"))
library(ggplot2)
library(ggpubr)
library(tidyverse)
library(broom)
library(AICcmodavg)
crop.data <- read.csv("F:/crop.yield.csv", header = TRUE, colClasses
= c("factor","factor","factor", "factor","factor", "numeric"))
summary(crop.data)
Program 8: Fit a simple linear regression model using the lm() function.
df <- data.frame(x=c(1,2,3,4,5),
y=c(1,5,8,15,26))
linear_model <- lm(y~x^2, data=df)
summary(linear_model)
Output:
Program 9: Program Experiment the steps to connect with a data source and choose between
live connection and extract using R
1.CSV Files:
data <- read.csv("path/to/your/file.csv")
2.Excel Files:
library(readxl)
data <- read_excel("path/to/your/file.xlsx")
4. Google Sheets:
library(googlesheets4)
gs4_auth()
Program 10: Program to Add Web Images Dynamically to Worksheets using R
Program 11: Program to Organize and Customize Fields in the Data Pane. using R
library(dplyr)
library(tidyr)
data<-data.frame(
ID=1:5,
Name=c("John", "Alice", "Bob", "Emily", "David"),
Age = c(25, 30, 35, 40, 45),
City = c("New York", "Los Angeles", "Chicago", "Houston","Miami"),
Income = c(50000, 60000, 70000, 80000, 90000)
)
print("Original Dataset:")
print(data)
organized_data<-data%>%
select(ID, Name, Age, Income, City)
print("Organized Dataset:")
print(organized_data)
Output:
Program 12: Perform Maps and Geographic Data Analysis using data analytics R
install.packages("ggplot2")
install.packages("sf")
install.packages("dplyr")
install.packages("leaflet")
library(ggplot2)
library(sf)
library(dplyr)
library(leaflet)
st_crs(shapefile)
st_crs(shapefile)
ggplot() +
geom_sf(data = shapefile)
ggplot() +
geom_sf(data = shapefile, fill = "lightblue", color = "black") +
labs(title = "Your Map Title", subtitle = "Your Map Subtitle",
caption = "Your Map Caption") +
theme_minimal()
ggplot() +
geom_sf(data = shapefile, fill = "lightblue", color = "black") +
geom_point(data = centroids, aes(x = X, y = Y), color = "red") +
labs(title = "Map with Centroids")
leaflet() %>%
addProviderTiles("Stamen.TonerLite") %>%
addPolygons(data = shapefile, fillOpacity = 0.5, color = "black")
Output:
# Sample data
data <- data.frame(
ID = c(1, 2, 3, 4),
Sales = c(100, 200, 150, 300),
Cost = c(50, 80, 60, 120)
)
print(data)
print(data)
print(data)
Output:
Program 14: Write a program to apply filters to dimensions and measures
library(dplyr)
data<-data.frame(
product =c('A','B','C','D'),
sales =c(100,200,150,300),
cost=c(50,80,60,120)
)
cat("Original data:\n")
print(data)
filtered_data <-data%>%
filter(sales>150)
cat("\nFiltered Data(Sales>150):\n")
print(filtered_data)
Output:
Program 15:
# Install and load necessary libraries
install.packages("sf") # Install sf package if you don't have
it
library(ggplot2)
library(sf)
library(dplyr)