R Programming Assignment Answers (1)
R Programming Assignment Answers (1)
**Unit 1 Answers**
1. Define vector.
A vector is a one-dimensional array that can hold multiple elements of the same data type, such as
2. What is R programming?
R is a programming language and software environment for statistical computing and graphics. It is
widely used for data analysis, statistical modeling, and data visualization.
- Numeric
- Integer
- Complex
- Logical
- Character
- Factor
- Date
- Data Frame
The common probability mass functions (PMF) are for discrete random variables, such as the
Binomial, Poisson, and Geometric distributions. It gives the probability that a discrete random
variable takes a specific value.
Advantages:
- Cross-platform compatibility
Disadvantages:
- Relational Operators: `==`, `!=`, `>`, `<`, `>=`, `<=` (e.g., `x == 10`)
- Logical Operators: `&`, `|`, `!` (e.g., `x > 5 & y < 10`)
A data frame is a two-dimensional, tabular structure in R that can store data of different types in
```R
df <- data.frame(Name = c("John", "Jane"), Age = c(23, 22), Gender = c("M", "F"))
```
9. What is an array? How array can be created and access array item with syntax and example.
```R
```
10. Explain amount of rows and columns in arrays and array length.
You can use `dim()` to get the dimensions of an array, and `length()` to get the total number of
elements.
```R
length(arr) # Returns 12
```
12. Explain how lists are created and elements are accessed.
A list can contain elements of different types. You can access elements using `[[ ]]` or `$`.
```R
```
13. Explain how to access matrix more than one row and more than one column.
You can access a range of rows and columns using the `[row_range, column_range]` indexing.
```R
matrix_data[1:2, 2:3] # Accesses first two rows and second to third columns
```
Non-numeric values include character, logical, or factor data types. The `is.numeric()` function
```R
x <- 5
```
```R
```
**Unit 2 Answers**
Conditional statements allow branching logic. The common conditional statements are `if`, `else if`,
and `else`.
```R
if (x > 10) {
} else {
```
```R
for (i in 1:5) {
print(i)
}
```
To read files, R provides several functions like `read.csv()`, `read.table()`, and `readRDS()`.
```R
```
```R
write.csv(data, "output.csv")
```
Functions like `file.exists()`, `dir.exists()`, and `file.info()` help in file and directory manipulation.
```R
return(a + b)
```
- Built-in functions
- Anonymous functions
```R
return(y + 1)
return(inner(x) * 2)
```
```R
return(length * width)
return(calculate_area())
```
R does not have formal exception handling, but you can use `try()` and `tryCatch()` to handle errors.
```R
10 / 0
}, warning = function(w) {
"Warning caught"
}, error = function(e) {
"Error caught"
})
```
11. Write a short note on stop(), warning() and warn option with an example and how to set warn
option.
```R
```
```R
install.packages("ggplot2")
```
13. Write a program to calculate the age of a person based on their birth date.
```R
age <- function(birth_date) {
return(floor(age))
```
```R
add(3, 4)
```