R_Lab_rec_final
R_Lab_rec_final
NAME :
REGISTER NO. :
COURSE : M.C.A
YEAR / SEM / SEC : II/ III /
DEPARTMENT OF COMPUTER APPLICATIONS
BONAFIDE CERTIFICATE
PAGE
S.NO DATE CONTENTS NO. SIGNATURE
Operations On Matrices 4
1. (Addition, Subtraction, Multiplication)
Operations On Vectors 7
2.
AIM:
ALGORITHM:
Step 3: Use the matrix function to create matrices from the vectors.
Step 4: Ensure both matrices have the same dimensions, then use the + operator to add the
matrices.
Step 5: Ensure both matrices have the same dimensions, then use the - operator to subtract the
matrices.
Step 6: Ensure the columns of first matrix is same as rows of second matrix to multiply the
matrices.
PROGRAM:
RESULT:
AIM:
ALGORITHM:
Step 5: Perform statistical operations like mean, median, sum, and product.
PROGRAM:
vec<-c(1,2,3,4,5)
multivec <- vec*2
multivec
vec2<-c(6,7,8,9,10)
vector_add <- vec+vec2
vector_mul <- vec*vec2
vector_mul
sum(vec_rep)
OUTPUT:
> vec<-c(1,2,3,4,5)
> multivec <- vec*2
> multivec
[1] 2 4 6 8 10
> vec2<-c(6,7,8,9,10)
> vector_add <- vec+vec2
> vector_mul <- vec*vec2
> vector_mul
[1] 6 14 24 36 50
> vector_sub <- vec2-vec
> vector_sub
[1] 5 5 5 5 5
> vector_div <- multivec/vec
> vec_seq <- seq(from=1,to=20,length=30)
> vec_seq
RESULT:
AIM:
To write a program to create data frames, Bar- chart & histogram in R programming
ALGORITHM:
Step 2: Use the data.frame() function to combine the vectors into a data frame.
Step 3: Identify the categorical data and corresponding frequencies that you want to visualize in
a bar chart.
Step 4: Use the barplot() function for basic bar charts or ggplot2 for more customization.
Step 5: Identify the numerical data that you want to visualize in a histogram.
setwd("E:\\sample")
getwd()
sample = read.csv("k1.csv")
data <- read.csv("k1.csv")
head(sample)
salarybar<-c(names,salaries, byrow=FALSE)
barplot(data$salary,main="SALARY", ylab='salary',
xlab ="name", col="blue",
horiz=FALSE,beside=TRUE)
hist(data$salary)
colnames<-c(data$name)
colnames
rowname<-c(data$sa)
OUTPUT
RESULT:
AIM:
ALGORITHM:
Step 2: Decide the type of random numbers you want to generate (uniform, normal, binomial).
Step 4: Use the appropriate function to generate random numbers based on the chosen
distribution and specified parameters.
df <- data.frame("Serial_number" = 1:5, "Age" = c(20, 21, 17, 23, 19), "Name" = c("raja","ramu",
"ravi", "raghu", "rajesh"))
# Sort by age ascending order
newdataAsc <- df[order(df$Age),]
newdataAsc
# sorting is descending order
newdataDsc <- df[order(-df$Age),]
newdataDsc
OUTPUT:
RESULT:
AIM:
To write a program for calculating mean, median, standard deviation, and variance for a given set
of data.
ALGORITHM:
Step 3: The mean is the average of all numbers. Use the mean function in R to generate mean
value.
Step 4: The median is the middle value of the sorted data. Use the median function in R to
calculate median value.
Step 5: The standard deviation measures the amount of variation or dispersion in a set of values.
Use the SD function in R to compute Standard deviation.
Step 6: The variance is the square of the standard deviation. Use the var function in R to
calculate Variance.
Step 7: Display the calculated mean, median, standard deviation, and variance.
setwd("E:\\Sample")
getwd()
sample2 = read.csv("k2.csv")
data <- read.csv("k2.csv")
data
mean(data$x)
median(data$x)
sd(data$x)
var(data$x)
RESULT:
AIM:
ALGORITHM:
Step 2: Create a function that takes a single argument ‘n’, which represents the number for
which we want to calculate the factorial.
Step 5: If n is greater than 1 then the function multiplies n and recursion of the function n-1
until n becomes 0 or 1.
Step 6: The function will eventually reach the base case after several calls, returning the final
computed factorial.
OUTPUT:
RESULT:
AIM:
To plot a scatter graph and evaluating linear model to predict car sales using advertisement cost.
ALGORITHM:
Step 2: Create a directory "sample", then set the working directory using setwd() in R.
Step 3: Ensure that you have k1.csv file in the E:/sample directory with the required data, then
read the CSV file into a data frame.
Step 5: Extract columns from the original data frame and create a new data frame.
Step 6: Create a scatter plot to visualize the relationship between car sales and advertisement
cost.
Step 7: Create a linear model to predict car sales based on advertisement cost.
Step 8: Get a summary of the linear model to see the coefficients and statistical metrics.
setwd("E:\\Sample")
getwd()
sample1 = read.csv("k3.csv")
data <- read.csv("k3.csv")
head(sample1)
# Create a data frame.
AIM:
ALGORITHM:
Step 3: Use dnorm to calculate the density function values over a range of x-values if we want
to plot the PDF (Probability Density Function).
Step 4: Use base R plotting functions like plot() and lines(), or advanced functions from
packages.
Step 5: Add titles, axis labels, and other enhancements for better visualization.
OUTPUT:
RESULT:
AIM:
ALGORITHM:
Step 2: Import the data into R from a file (such as Excel or other sources).
Step 3: Check the data structure for any missing values and ensure the data is in the correct
format for time series analysis.
Step 4: If the data is not already in time series format, convert it using the ts() function.
Step 5: Use the plot() function or other specialized plotting functions from relevant packages to
create a time series graph.
Step 6: Add a title, labels, and other annotations to make the plot more informative.
sales_ice <- c(450, 613, 466, 205.7,571.0, 622.0, 851.4, 621.4,875.3,979.7, 927.5,14.45)
sales_cooldrink <- c(550, 713, 566, 687.2,110, 120, 72.4,814.4,423.5, 98.7, 741.4,345.3)
print(sales.timeseries)
OUTPUT:
RESULT:
AIM:
ALGORITHM:
x <- sample(c(1:100),size=20,replace=TRUE)
y <- sample(c(1:100),size=20,replace=TRUE)
RESULT: