Sample Programs to Explore Basic Concepts in R
Sample Programs to Explore Basic Concepts in R
These programs help students become familiar with variables, data types, input/output functions, keywords, operators, and packages in R.
Program 1: Variable Assignment in R
Aim
To demonstrate different methods of variable assignment.
Program
# Using equal operator
x = 10
# Using leftward assignment
y <- 20
# Using rightward assignment
30 -> z
print(x)
print(y)
print(z)
Output
[1] 10
[1] 20
[1] 30
Program 2: Exploring Basic Data Types
Aim
To study different data types and determine their classes.
Program
num <- 10.5
integer_num <- 10L
name <- "John"
flag <- TRUE
complex_num <- 3 + 4i
print(class(num))
print(class(integer_num))
print(class(name))
print(class(flag))
print(class(complex_num))
Output
[1] "numeric"
[1] "integer"
[1] "character"
[1] "logical"
[1] "complex"
Program 3: Type Conversion
Aim
To demonstrate explicit type conversion.
Program
x <- "100"
num <- as.numeric(x)
str <- as.character(num)
int_num <- as.integer(num)
print(num)
print(str)
print(int_num)
Output
[1] 100
[1] "100"
[1] 100
Program 4: Basic Input and Output Functions
Aim
To study input and output functions.
Program
name <- readline("Enter your name: ")
message("Data entered successfully")
msg <- sprintf("Welcome %s", name)
print(msg)
cat("Thank you", name)
Sample Output
Enter your name: Alice
Data entered successfully
[1] "Welcome Alice"
Thank you Alice
Program 5: Arithmetic Operators
Aim
To demonstrate arithmetic operators.
Program
a <- 20
b <- 3
cat("Addition =", a+b, "\n")
cat("Subtraction =", a-b, "\n")
cat("Multiplication =", a*b, "\n")
cat("Division =", a/b, "\n")
cat("Modulus =", a%%b, "\n")
cat("Integer Division =", a%/%b)
Output
Addition = 23
Subtraction = 17
Multiplication = 60
Division = 6.666667
Modulus = 2
Integer Division = 6
Program 6: Relational and Logical Operators
Aim
To study relational and logical operators.
Program
x <- 10
y <- 20
print(x < y)
print(x > y)
print(TRUE & FALSE)
print(TRUE | FALSE)
print(!TRUE)
Output
[1] TRUE
[1] FALSE
[1] FALSE
[1] TRUE
[1] FALSE
Program 7: Variables and Workspace Management
Aim
To study variable-related functions.
Program
name <- "Alice"
age <- 20
print(ls())
print(class(name))
rm(age)
print(ls())
Output
[1] "age" "name"
[1] "character"
[1] "name"
Program 8: Vector Operations Using Operators
Aim
To demonstrate vectorized operations.
Program
x <- c(1,2,3)
y <- c(4,5,6)
print(x+y)
print(x*y)
print(3 %in% x)
Output
[1] 5 7 9
[1] 4 10 18
[1] TRUE
Program 9: Installing and Using Packages
Aim
To study package installation and usage in R.
Theory
A package is a collection of functions, datasets, and documentation designed to perform specific tasks. Packages extend the capabilities of R.
Program
# Install package (execute only once)
install.packages("ggplot2")
# Load package
library(ggplot2)
# Create sample data
student <- data.frame(
Name=c("A","B","C","D"),
Marks=c(80,90,75,85)
)
# Plot bar graph
ggplot(student, aes(x=Name, y=Marks)) + geom_bar(stat="identity")
Output
A bar chart showing student marks.
Program 10: Using dplyr Package
Aim
To demonstrate data manipulation using the dplyr package.
Program
# Install once
install.packages("dplyr")
# Load package
library(dplyr)
student <- data.frame(
Name=c("John","Mary","Alex"),
Marks=c(80,95,70)
)
result <- student %>%
filter(Marks > 75)
print(result)
Output
Name Marks
1 John 80
2 Mary 95
Program 11: Area of a Circle Given the Diameter
Problem Statement
Write an R program to find the area of a circle when the diameter is given as input.
Formula
Since the radius is half of the diameter,
Therefore,
Algorithm
- Read the diameter of the circle.
- Calculate the radius as diameter/2.
-
Compute the area using the formula
- Display the area.
Program
# Input diameter
diameter <- as.numeric(readline("Enter the diameter: "))
# Calculate radius
radius <- diameter / 2
# Calculate area
area <- pi * radius^2
# Display result
cat("Area of the circle =", area)
Sample Output
Enter the diameter: 10
Area of the circle = 78.53982
Formatted Output Using sprintf()
diameter <- as.numeric(readline("Enter the diameter: "))
radius <- diameter / 2
area <- pi * radius^2
cat(sprintf("Area of the circle = %.2f", area))
Sample Output
Enter the diameter: 10
Area of the circle = 78.54Program 12: Reading 6 marks from a file and computing the %
# Read marks from file
marks <- scan("marks.txt")
# Calculate total
total <- sum(marks)
# Calculate average
average <- mean(marks)
# Calculate percentage
# Assuming each subject is out of 100 marks
percentage <- total/6
# Display results
cat("Marks =", marks, "\n")
cat("Total =", total, "\n")
cat("Average =", average, "\n")
cat("Percentage =", percentage)
Sample Output
Read 6 items
Marks = 78 85 90 88 76 92
Total = 509
Average = 84.83333
Percentage = 84.83333
Program 13: Using the dplyr Package
| |
Comments
Post a Comment