Sample Programs Using Arrays in R
Sample Programs Using Arrays in R
Arrays are used to represent multidimensional homogeneous data. The following programs illustrate practical numerical applications of arrays suitable for undergraduate laboratories.
1. Student Marks Analysis Across Semesters
Problem
Marks of 3 students in 4 subjects over 2 semesters are stored in a three-dimensional array.
Find:
- Total marks of each student in each semester.
- Average marks in each semester.
- Highest mark in the entire array.
Program
marks <- array(
c(85,90,78,88,
92,80,86,95,
75,82,85,79,
89,91,83,90,
94,85,88,96,
80,84,87,81),
dim=c(3,4,2)
)
print(marks)
# Total marks of students in each semester
student_totals <- apply(marks, c(1,3), sum)
cat("Student Totals\n")
print(student_totals)
# Average marks in each semester
semester_avg <- apply(marks, 3, mean)
cat("Semester Averages\n")
print(semester_avg)
# Highest mark
cat("Highest Mark =", max(marks))
2. Monthly Sales Analysis
Problem
Sales of 3 products in 4 quarters over 2 years are stored in an array.
Find:
- Total sales for each product.
- Total sales for each year.
- Product having maximum total sales.
Program
sales <- array(
c(100,120,110,130,
140,150,160,155,
90,100,95,105,
110,125,115,140,
150,165,170,175,
95,110,100,120),
dim=c(3,4,2)
)
# Total sales of products
product_sales <- apply(sales,1,sum)
cat("Product Sales\n")
print(product_sales)
# Total sales of years
year_sales <- apply(sales,3,sum)
cat("Year Sales\n")
print(year_sales)
cat("Highest Selling Product =", which.max(product_sales))
3. Temperature Monitoring
Problem
Daily temperatures are recorded for 7 days during 3 weeks.
Find:
- Average temperature each week.
- Maximum temperature recorded.
- Minimum temperature recorded.
Program
temp <- array(
c(30,31,29,32,33,31,30,
29,30,31,32,34,33,31,
28,29,30,31,32,30,29),
dim=c(7,3)
)
print(temp)
weekly_avg <- apply(temp,2,mean)
cat("Weekly Average Temperatures\n")
print(weekly_avg)
cat("Maximum Temperature =", max(temp), "\n")
cat("Minimum Temperature =", min(temp))
4. RGB Image Representation
Problem
Represent a small color image using a three-dimensional array and calculate average intensity in each channel.
Program
image <- array(
c(
255,0,0,255,
0,255,255,0,
100,120,130,140,
150,160,170,180,
50,60,70,80,
90,100,110,120
),
dim=c(2,4,3)
)
cat("Average Red Channel =",
mean(image[,,1]), "\n")
cat("Average Green Channel =",
mean(image[,,2]), "\n")
cat("Average Blue Channel =",
mean(image[,,3]), "\n")
Concepts Demonstrated
- Three-dimensional arrays
- Slicing arrays
- Image representation
5. Rainfall Analysis
Problem
Rainfall data for 12 months over 3 years are stored in an array.
Find:
- Average rainfall each year.
- Month having maximum rainfall.
- Total rainfall over all years.
Program
rainfall <- array(
sample(50:200,36),
dim=c(12,3)
)
print(rainfall)
# Average rainfall of years
year_avg <- apply(rainfall,2,mean)
cat("Average Rainfall per Year\n")
print(year_avg)
# Maximum rainfall
cat("Maximum Rainfall =", max(rainfall), "\n")
# Total rainfall
cat("Total Rainfall =", sum(rainfall))
6.Population Data Analysis
Problem
Population of 4 cities over 5 years for 2 states is stored in a 3D array.
Find:
- State-wise population.
- City-wise average population.
- Maximum population recorded.
Program
population <- array(
sample(1000:5000,40),
dim=c(4,5,2)
)
# State totals
state_pop <- apply(population,3,sum)
# City averages
city_avg <- apply(population,1,mean)
cat("State Population\n")
print(state_pop)
cat("City Average Population\n")
print(city_avg)
cat("Maximum Population =", max(population))
Comments
Post a Comment