Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

R Programming Assignment Answers (1)

The document provides answers to an R programming assignment covering fundamental concepts such as vectors, data types, operators, data frames, arrays, and functions. It includes explanations of conditional statements, looping statements, file reading/writing, and error handling in R. Additionally, it discusses advantages and disadvantages of R, types of functions, and examples of various programming constructs.

Uploaded by

Dhananjay S P
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

R Programming Assignment Answers (1)

The document provides answers to an R programming assignment covering fundamental concepts such as vectors, data types, operators, data frames, arrays, and functions. It includes explanations of conditional statements, looping statements, file reading/writing, and error handling in R. Additionally, it discusses advantages and disadvantages of R, types of functions, and examples of various programming constructs.

Uploaded by

Dhananjay S P
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

R Programming Assignment - Answers

**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

numeric, character, or logical.

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.

3. Write the different classes used in R programming?

- Numeric

- Integer

- Complex

- Logical

- Character

- Factor

- Date

- Data Frame

4. What is the common probability mass function?

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.

5. Explain the features of R programming.

- Open-source and free

- Extensive libraries for statistics and data analysis

- Built-in support for data visualization

- Cross-platform (Windows, Mac, Linux)

- Strong community support

- Supports statistical methods, machine learning, and data manipulation

6. Mention the advantages and disadvantages of R programming.

Advantages:

- Large set of libraries

- Great for statistical analysis

- Cross-platform compatibility

Disadvantages:

- Can be slower for large datasets

- Limited support for GUI-based development

- Less efficient for general-purpose programming

7. Explain the types of operators in R with examples.

- Arithmetic Operators: `+`, `-`, `*`, `/`, `^` (e.g., `3 + 4`)

- Relational Operators: `==`, `!=`, `>`, `<`, `>=`, `<=` (e.g., `x == 10`)

- Logical Operators: `&`, `|`, `!` (e.g., `x > 5 & y < 10`)

- Assignment Operators: `<-`, `=`, `<<-` (e.g., `x <- 5`)

- Miscellaneous Operators: `:` (sequence), `%in%` (membership), `%%` (modulo)


8. Explain the characteristics of data frame with an example.

A data frame is a two-dimensional, tabular structure in R that can store data of different types in

each column. It is similar to a table in a database or a spreadsheet.

```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.

An array is a multi-dimensional collection of elements of the same type.

```R

arr <- array(1:12, dim = c(3, 4)) # 3x4 array

arr[2, 3] # Accesses the element in the 2nd row, 3rd column

```

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

dim(arr) # Returns 3 4 (3 rows, 4 columns)

length(arr) # Returns 12

```

11. Explain the different types of atomic vectors with examples.

Atomic vectors in R are the simplest types of data structures:

- Logical: `x <- c(TRUE, FALSE)`

- Integer: `x <- c(1L, 2L)`


- Numeric: `x <- c(1.5, 2.3)`

- Character: `x <- c("A", "B")`

- Complex: `x <- c(1+2i, 3+4i)`

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

my_list <- list(name = "John", age = 23, is_student = TRUE)

my_list$name # Accesses the name element

```

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 <- matrix(1:12, nrow = 3, ncol = 4)

matrix_data[1:2, 2:3] # Accesses first two rows and second to third columns

```

14. What is Non Numeric value? Explain the is.numeric() function.

Non-numeric values include character, logical, or factor data types. The `is.numeric()` function

checks if the element is numeric.

```R

x <- 5

is.numeric(x) # Returns TRUE

```

15. How do you add a new column to a data frame in R?


You can add a column by directly assigning it to the data frame.

```R

df$New_Column <- c(1, 2)

```

**Unit 2 Answers**

1. Explain conditional statements with syntax and example.

Conditional statements allow branching logic. The common conditional statements are `if`, `else if`,

and `else`.

```R

if (x > 10) {

print("Greater than 10")

} else {

print("Less than or equal to 10")

```

2. Explain looping statements with syntax and example.

R supports several looping statements: `for`, `while`, and `repeat`.

```R

for (i in 1:5) {

print(i)

}
```

3. Explain reading files in details.

To read files, R provides several functions like `read.csv()`, `read.table()`, and `readRDS()`.

```R

data <- read.csv("data.csv")

```

4. Explain writing files in details.

To write data to a file, R provides functions like `write.csv()`, `write.table()`.

```R

write.csv(data, "output.csv")

```

5. Write a note on file and directory functions.

Functions like `file.exists()`, `dir.exists()`, and `file.info()` help in file and directory manipulation.

6. Explain function in R with syntax.

Functions in R are defined using `function()` keyword.

```R

add <- function(a, b) {

return(a + b)

```

7. Describe briefly on types of functions in R.

Types of functions include:


- User-defined functions

- Built-in functions

- Anonymous functions

8. Write a short note on nested functions with an example.

A nested function is one defined inside another function.

```R

outer <- function(x) {

inner <- function(y) {

return(y + 1)

return(inner(x) * 2)

```

9. Write a program to calculate the area of rectangle using nested functions.

```R

area <- function(length, width) {

calculate_area <- function() {

return(length * width)

return(calculate_area())

```

10. Explain exceptions in detail with an example.

R does not have formal exception handling, but you can use `try()` and `tryCatch()` to handle errors.
```R

result <- tryCatch({

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.

- `stop()`: Stops execution and generates an error.

- `warning()`: Generates a warning message.

- `warn` option controls the behavior of warnings.

```R

options(warn = 2) # Treat warnings as errors

```

12. Explain how to install a new package.

You can install a package using the `install.packages()` function.

```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) {

current_date <- Sys.Date()

age <- as.numeric(difftime(current_date, birth_date, units = "weeks")) / 52.25

return(floor(age))

```

14. How do you call a function in R?

You call a function by passing the required arguments.

```R

add(3, 4)

```

You might also like