R Prgms
R Prgms
ALGORITHM
CODE
ALGORITHM
CODE
OUTPUT
3. Check if a Number is Positive, Negative or Zero
ALGORITHM
CODE
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
print(numeric_vector)
print(character_vector)
numeric_matrix <- matrix(1:12, nrow = 3, ncol = 4, byrow = TRUE) # 3x4 matrix filled row-
wise
print(numeric_matrix)
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
2. Assign variables:
3. Plotting:
- Set the plot title, x-axis label, y-axis label, plot symbol, and frame
CODE
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:
ALGORITHM
1. Logical Statements:
- 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`.
- Add a new column named 'Grade' to the `combined_df` dataframe using the `cbind` function.
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")
}
# Second dataframe
df2 <- data.frame(
ID = 4:5,
Name = c("David", "Emily"),
Score = c(88, 95)
)
# Plotting a graph
# Creating vectors for x and y
x <- c(1, 2, 3, 4, 5)
y <- c(2, 4, 6, 8, 10)