0% found this document useful (0 votes)
41 views12 pages

R Prgms

The document discusses various concepts in R programming such as: 1) Using recursion to calculate factorials of numbers. 2) Creating multiplication tables by taking user input and iterating with a for loop. 3) Checking if a number is positive, negative, or zero using conditional statements. 4) Creating vectors and matrices using functions like c() and matrix(). 5) Importing and visualizing the iris dataset with a scatter plot of sepal length vs width. 6) Demonstrating logical statements, combining dataframes with rbind and cbind, adding factors, and plotting graphs.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views12 pages

R Prgms

The document discusses various concepts in R programming such as: 1) Using recursion to calculate factorials of numbers. 2) Creating multiplication tables by taking user input and iterating with a for loop. 3) Checking if a number is positive, negative, or zero using conditional statements. 4) Creating vectors and matrices using functions like c() and matrix(). 5) Importing and visualizing the iris dataset with a scatter plot of sepal length vs width. 6) Demonstrating logical statements, combining dataframes with rbind and cbind, adding factors, and plotting graphs.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

COMPUTER APPLICATION PRACTICAL IV – ANALYSIS WITH SPSS & R

1. Find Factorial of a number using recursion

ALGORITHM

1. Define a function factorial_recursive that takes an integer n as input.


2. Check if the input number n is equal to 0 or 1. If true, return 1 (base case for factorial).
3. If the number n is not 0 or 1, recursively call the factorial_recursive function with n-1 and
multiply it with n.
4. Continue this recursion until n reaches 0 or 1, and then return the final computed factorial
value.
5. Prompt the user to input a number.
6. Check if the input number is non-negative.
7. If the number is non-negative, call the factorial_recursive function with the user-input
number.
8. Display the computed factorial of the entered number as the output.
9. Stop the program.

CODE

# Function to calculate factorial using recursion


factorial_recursive <- function(n) {
if (n == 0 || n == 1) {
return(1)
} else {
return(n * factorial_recursive(n - 1))
}
}

# Taking user input for the number


num <- as.integer(readline(prompt = "Enter a number to calculate its factorial: "))

# Checking if the number is non-negative


if (num < 0) {
print("Factorial is not defined for negative numbers.")
} else {
result <- factorial_recursive(num)
cat("Factorial of", num, "is", result)
}
OUTPUT
2. Write program to calculate Multiplication Table using R

ALGORITHM

1. Prompt the user to input a number.


2. Read the user input and store it in a variable num.
3. Start a loop from 1 to 10.
a. Inside the loop:
i. Calculate the product of num and the loop variable i.
ii. Print the multiplication table entry in the format: num * i = result.

CODE

# R Program to find the multiplicationtable (from 1 to 10)

# take input from the user


num = as.integer(readline(prompt = "Enter a number: "))

# use for loop to iterate 10 times


for(i in 1:10) {
print(paste(num,'x', i, '=', num*i))
}

OUTPUT
3. Check if a Number is Positive, Negative or Zero

ALGORITHM

1. Prompt the user to input a number.


2. Read the input and store it in a variable num.
3. Check the value of num:
a. If num is greater than 0, print "Positive number".
b. If num is equal to 0, print "Zero".
c. If num is less than 0, print "Negative number".

CODE

num = as.double(readline(prompt="Enter a number: "))


if(num > 0) {
print("Positive number")
} else {
if(num == 0) {
print("Zero")
} else {
print("Negative number")
}
}

OUTPUT
4. Creating vector and matrices using R program

ALGORITHM

1. Numeric Vector:
a. Use the c() function to concatenate numeric elements into a vector.
2. Character Vector:
a. Use the c() function to concatenate character elements into a vector.
3. Numeric Matrix:
a. Use the matrix() function to create a numeric matrix.
b. Specify the range of values (1:12), the number of rows (nrow), the number of
columns (ncol), and the filling method (byrow or bycolumn).
4. Character Matrix:
a. Use the matrix() function to create a character matrix.
b. Specify the elements (c("a", "b", "c", "d")), the number of rows (nrow), and the
number of columns (ncol).

CODE

# Creating a numeric vector

numeric_vector <- c(1, 2, 3, 4, 5)

print(numeric_vector)

# Creating a character vector

character_vector <- c("apple", "banana", "orange")

print(character_vector)

# Creating a numeric matrix

numeric_matrix <- matrix(1:12, nrow = 3, ncol = 4, byrow = TRUE) # 3x4 matrix filled row-
wise

print(numeric_matrix)

# Creating a character matrix

character_matrix <- matrix(c("a", "b", "c", "d"), nrow = 2, ncol = 2)

print(character_matrix)

OUTPUT
5.
5. Import and Visualize data using scatter plots

Aim:

To import the iris dataset and visualize the relationship with Sepal Width and Sepal Length

ALGORITHM

1. Load the iris dataset:


Use the `data()` function to load the built-in `iris` dataset into R.

2. Assign variables:

- Assign the `Sepal.Width` column of the `iris` dataset to variable `x`.

- Assign the `Sepal.Length` column of the `iris` dataset to variable `y`.

3. Plotting:

- Set the plot title, x-axis label, y-axis label, plot symbol, and frame

CODE

data(iris) # Load the iris dataset

x <- iris$Sepal.Width # Use Sepal Width as x variable

y <- iris$Sepal.Length # Use Sepal Length as y variable

plot(x, y, main="Sepal Length vs Sepal Width", xlab="Sepal Width", ylab="Sepal Length", pch=10,
frame=TRUE)
OUTPUT
6. Logical statements, cbind/rbind command in R and Create dataset using dataframes and factors
and plot a graph.

Aim:

To demonstrate various concepts including logical statements, creating and manipulating


data frames using functions like `rbind` and `cbind`, and plotting a graph.

ALGORITHM

1. Logical Statements:

- Define a variable `number`.

- Check if `number` is even or odd using the modulo operator (`%%`).

- Print whether the number is even or odd.

2. Creating Dataframes and Combining them:

- Create two data frames `df1` and `df2` with some sample data.

- Use the `rbind` function to combine these data frames row-wise into `combined_df`.

3. Adding a New Column to the Combined Dataframe:

- Add a new column named 'Grade' to the `combined_df` dataframe using the `cbind` function.

- The values for this column are specified as factors.

4. Plotting a Graph:

- Create vectors `x` and `y` containing sample data for the x and y-axis values.

- Plot a line graph using the `plot` function with `x` and `y` as inputs.

- Customize the plot with a title, labels for the x and y axes, line type, color, and line width.

CODE
# Logical statements
# Checking if a number is even or odd
number = 15
if (number %% 2 == 0) {
print("Number is even")
} else {
print("Number is odd")
}

# Creating dataframes and using rbind and cbind functions


# First dataframe
df1 <- data.frame(
ID = 1:3,
Name = c("Alice", "Bob", "Charlie"),
Score = c(85, 92, 78)
)

# Second dataframe
df2 <- data.frame(
ID = 4:5,
Name = c("David", "Emily"),
Score = c(88, 95)
)

# Using rbind to combine dataframes row-wise


combined_df <- rbind(df1, df2)
print(combined_df)

# Adding a new column using cbind


combined_df <- cbind(combined_df, Grade = factor(c("A", "B", "C", "B", "A")))
print(combined_df)

# Plotting a graph
# Creating vectors for x and y
x <- c(1, 2, 3, 4, 5)
y <- c(2, 4, 6, 8, 10)

# Plotting the graph


plot(x, y, type = "l", col = "blue", lwd = 2, main = "Linear Graph", xlab = "X-axis", ylab = "Y-axis")
OUTPUT

You might also like