R LAB Manual
R LAB Manual
R LAB Manual
OUTPUT:
Original Vector: 2 7 10 15 8 3 6
Classification: Even Odd Even Odd Even Odd Even
4. Paste the Code: Paste the copied code into the newly created R script file in RStudio.
5. Run the Code: To run the code, you can either:
The output will be displayed in the Console panel, as explained in my previous response,
showing the original vector and the classification results.
OUTPUT:
Original Vector: 2 7 10 15 8 3 6
OUTPUT
No errors encountered.
Here's a simple R program that uses a for loop and stops when a certain condition is met. In this
example, the program generates random numbers and stops when a number greater than 0.9 is
encountered, printing an error message.
ERROR ENCOUNTED
If you want the program to print an error message whenever a random number greater than 0.9 is
encountered but still continue running and generating numbers, you can make a slight
OUTPUT
Error: Random number exceeded the threshold ( 0.9273901 )
Error: Random number exceeded the threshold ( 0.9296098 )
With this modification, the program will continue running and generating
random numbers even when it encounters a random number greater than 0.9. It
will print an error message for each occurrence of such a number but won't
exit the loop until it completes all iterations. After the loop, it will
still check if an error occurred and print a message accordingly.
if (num >= 0) {
result <- factorial(num)
cat("Factorial of", num, "is", result, "\n")
} else {
OUTPUT
F:\R-Program\lab>Rscript factorial.R 5
Factorial of 5 is 120
F:\R-Program\lab>Rscript factorial.R 3
Factorial of 3 is 6
F:\R-Program\lab>Rscript factorial.R 4
Factorial of 4 is 24
F:\R-Program\lab>Rscript factorial.R -2
OUTPUT :
T-Test Results:
> cat(" - p-value:", t_result$p.value, "\n")
- p-value: 0.0005245777
> cat(" - t-statistic:", t_result$statistic, "\n")
- t-statistic: -5.848077
> cat(" - Degrees of Freedom:", t_result$parameter, "\n")
- Degrees of Freedom: 7.360587
> cat(" - Confidence Interval (95%): [", t_result$conf.int[1], ",",
t_result$conf.int[2], "]\n")
- Confidence Interval (95%): [ -15.96412 , -6.835883 ]
> cat(" - Test Conclusion:")
- Test Conclusion:> if (t_result$p.value < 0.05) {
+ cat(" Reject the null hypothesis (significant difference)\n")
+ } else {
+ cat(" Fail to reject the null hypothesis (no significant difference)\n")
+ }
Reject the null hypothesis (significant difference)
tapply and sapply are two useful functions in R for working with data, especially when dealing
with grouped data or applying functions to elements of vectors. Here's an explanation of each:
1. tapply:
tapply stands for "table apply." It is used to apply a function to subsets of a vector or
array, split by one or more factors. This is particularly useful when you want to compute
summary statistics or perform operations on data grouped by certain categories.
tapply returns an array where the values are the results of applying the function FUN to each
group defined by the factors.
Example
BCA- V- R-LAB MANUAL Page 11
SMT. KUMUDBEN DARBAR COLLEGE OF COMMERCE,SCIENCE &
MANAGEMENT STUDIES
# Calculate the mean age for different genders
2. sapply:
sapply stands for "simplify apply." It is a more generalized version of lapply and is used to
apply a function to each element of a list or vector. The result is typically simplified into a vector
or array when possible.
sapply(X, FUN)
sapply returns a vector, matrix, or array, depending on the structure of the results.
Example
In summary, tapply is mainly used for applying functions to grouped data defined by factors,
while sapply is used for applying functions to elements of a list or vector. Both functions are
handy for data manipulation and analysis in R.
OUTPUT
Stationary Distribution:
> print(current_state)
[,1]
[1,] 0.5
[2,] 0.5
[,1]
[1,] 0.625
[2,] 0.375
if (arr[mid] == target) {
return(mid) # Found the target element
} else if (arr[mid] < target) {
left <- mid + 1
} else {
right <- mid - 1
}
}
# Example data
data <- c(5, 8, 1, 6, 3, 7, 2, 4)
target <- 6
OUTPUT
In this example, we'll read data from an existing text file, perform a simple
operation on the data, and then write the result to a new text file.
First create a text file input.txt in the same location as the prg8.R script and type
“Hello, this is a test.” And save it as shown below
# Create a plot
plot(x, y, type = "o", main = "Line Chart", xlab = "X-
axis", ylab = "Y-axis", col = "blue")
# Create a histogram
hist(x, main = "Histogram", xlab = "Value", ylab =
"Frequency", col = "lightgreen")
# Create a boxplot
boxplot(x, main = "Boxplot", ylab = "Values", col =
"orange")
# Create a scatterplot
plot(x, y, main = "Scatterplot", xlab = "X-axis", ylab =
"Y-axis", col = "green", pch = 19)
OUTPUT
# Create a histogram
hist(data,
main = "Sample Histogram",
xlab = "Values",
ylab = "Frequency",
col = "skyblue",
border = "black",
breaks = 5
)
OUTPUT
R doesn't have a specific function called LineChart() for creating line charts. However, you can
create a simple line chart using the base R graphics system with the plot() and lines()
functions. Here's a basic example
# Add points
points(x, y, col = "red", pch = 19)
# Add gridlines
grid()
OUTPUT
# Add a title
title(main = "Sample Pie Chart")
OUTPUT
# Create a boxplot
boxplot(data,
col = c("blue", "green"),
main = "Sample Boxplot",
xlab = "Groups",
ylab = "Values")
# Add a legend
legend("topright", legend = c("Group1", "Group2"), fill =
c("blue", "green"))
OUTPUT
You can use the plot() function to create a scatterplot with your data
# Create a scatterplot
plot(x, y,
xlab = "X-axis",
ylab = "Y-axis",
main = "Sample Scatterplot",
col = "blue",
pch = 19
)
OUTPUT
Data set= 3, 3, 3, 4, 4, 4, 5, 5, 5
In this dataset:
Mean: Mean = (3 + 3 + 3 + 4 + 4 + 4 + 5 + 5 + 5) / 9 = 36 / 9 = 4
Mode: The mode is the most frequently occurring value, and in this dataset, the
value 4 appears most frequently.
Program
# Create the dataset
data <- c(3, 3, 3, 4, 4, 4, 5, 5, 5)
OUTPUT
> cat("Mean:", mean_value, "\n")
Mean: 4
> cat("Median:", median_value, "\n")
Median: 4
> cat("Mode:", mode_value, "\n")
Mode: 3 4 5
10. Write a R program for with any dataset containing data frame objects,
and employ manipulating and analyzing data.
R program that demonstrates basic data manipulation and analysis using a dataset. In this
example, we'll use the built-in mtcars dataset, which contains information about various car
models:
# Find the car with the highest miles per gallon (mpg)
max_mpg_car <- mtcars[mtcars$mpg == max(mtcars$mpg), ]
OUTPUT
# Display the first few rows of the dataset
> head(mtcars)
mpg cyl disp hp drat wt qsec vs am gear carb
Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1
>
> # Calculate the mean and median of miles per gallon (mpg)
> mean_mpg <- mean(mtcars$mpg)
> median_mpg <- median(mtcars$mpg)
>
> # Display the mean and median
> cat("Mean MPG:", mean_mpg, "\n")
Mean MPG: 20.09062
> cat("Median MPG:", median_mpg, "\n")
Median MPG: 19.2
>
> # Find the car with the highest miles per gallon (mpg)
> max_mpg_car <- mtcars[mtcars$mpg == max(mtcars$mpg), ]
>
> # Display the car with the highest MPG
> cat("Car with the highest MPG:\n")
Car with the highest MPG:
> print(max_mpg_car)
OR IN SIMPLE WAY
Simple dataset and perform some basic data manipulation and analysis. We'll create a custom
dataset for this example and demonstrate operations such as summarizing data and subsetting.
Here's the R program:
OUTPUT
# Create a simple custom dataset (data frame)
> data <- data.frame(
+ StudentID = 1:10,
+ Age = c(22, 21, 23, 20, 22, 24, 19, 20, 21, 22),
+ Score = c(85, 92, 78, 65, 89, 73, 97, 81, 88, 90)
+ )
>
> # Display the dataset
> print(data)
StudentID Age Score
1 1 22 85
>
OUTPUT
# Create a custom dataset
> data <- data.frame(
+ Age = c(25, 30, 35, 40, 45, 50, 55, 60, 65, 70),
+ Income = c(35000, 40000, 45000, 50000, 55000, 60000, 65000, 70000, 75000, 80000),
+ Education = c(12, 14, 16, 16, 18, 20, 20, 22, 24, 24)
+ )
>
> # Display the dataset
> print(data)
Age Income Education
1 25 35000 12
2 30 40000 14
3 35 45000 16
4 40 50000 16
5 45 55000 18
6 50 60000 20
7 55 65000 20
8 60 70000 22
9 65 75000 24
10 70 80000 24
>
> # Create a linear regression model
> lm_model <- lm(Income ~ Age + Education, data = data)
>
> # Summary of the linear regression model
> summary(lm_model)
Call:
lm(formula = Income ~ Age + Education, data = data)
Residuals:
Min 1Q Median 3Q Max
-2.462e-12 -1.433e-12 -1.020e-12 1.287e-12 4.776e-12
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 1.000e+04 9.585e-12 1.043e+15 <2e-16 ***
Age 1.000e+03 4.255e-13 2.350e+15 <2e-16 ***
Education -1.124e-12 1.565e-12 -7.180e-01 0.496
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 2.736e-12 on 7 degrees of freedom
Multiple R-squared: 1, Adjusted R-squared: 1
F-statistic: 1.378e+32 on 2 and 7 DF, p-value: < 2.2e-16
Warning message:
>
END