0% found this document useful (0 votes)
15 views7 pages

Dar Assignment

Uploaded by

ajaytompe23
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)
15 views7 pages

Dar Assignment

Uploaded by

ajaytompe23
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/ 7

Here are the answers based on the provided document:

1. Definition of an Object in R:

An object in R refers to any data structure that stores values, expressions, or functions. Everything in
R is an object, including vectors, matrices, arrays, lists, and data frames.

Naming Rules for an Object:

Names should start with a letter, followed by letters, numbers, dots, or underscores.

Names cannot start with a number or contain special characters (e.g., @, !, etc.).

Reserved words cannot be used as object names (e.g., if, else, for).

2. Creating an Object with Values 1 to 10:

x <- 1:10

The object x now contains the sequence of numbers from 1 to 10.

Output of 1:12:

1:12

This creates a sequence of integers from 1 to 12:

[1] 1 2 3 4 5 6 7 8 9 10 11 12
3. Definition of a Function:

A function in R is a block of code written to carry out a specific task. Functions can take arguments,
process them, and return a result.

Full Body Syntax of a Function:

function_name <- function(arg1, arg2, ...) {

# Function body

result <- some_operations

return(result)

4. Creating a Function to Store Values from 1 to 10 in an Object Called scan:

store_scan <- function() {

scan <- 1:10

return(scan)

This function assigns the values from 1 to 10 to the object scan.

5. Using a Function to Perform Mathematical Operations with 1:2:

1:2 + 3:4

This adds corresponding elements from the two sequences, returning:

[1] 4 6
6. Different Types of Loop Statements with Syntax:

Repeat Loop:

a <- 1

repeat {

print(a)

a <- a + 1

if(a > 5) {

break

While Loop:

a <- 1

while (a <= 5) {

print(a)

a <- a + 1

For Loop:

for(i in 1:5) {

print(i)

7. Different Data Types in R:


Numeric: Holds decimal values, e.g., x = 10.5.

Integer: Holds whole numbers, e.g., y = as.integer(3).

Logical: Contains TRUE or FALSE values, e.g., z = TRUE.

Character: Holds text or string data, e.g., name = "R".

Complex: Holds complex numbers, e.g., w = 1+2i.

Functions to find the type of an object:

class(x)

typeof(x)

is.numeric(x), is.integer(x), is.character(x) etc.

8. Explanation of a Vector:

A vector is a one-dimensional array that can hold data of the same type. Vectors in R are useful for
storing sequences of values like numbers or characters.

Advantages of Using Vectors:

Vectors are efficient and support vectorized operations, meaning that operations on vectors are
applied to all elements without the need for loops.
9. Difference Between Matrix and Array:

Matrix: A two-dimensional data structure where each element has the same data type.

Array: A multi-dimensional generalization of a matrix where data can be spread across more than
two dimensions.

10. Difference Between Matrix and Data Frame:

Matrix: All elements must be of the same data type.

Data Frame: Can contain multiple data types across columns (e.g., numerical, characters, logical).

11. Difference Between Vectors and Lists:

Vector: Stores elements of the same type.

List: Can store elements of different types, including other lists.

12. Data Frame Creation:

df <- data.frame(

names = c("Alice", "Bob", "Charlie", "David", "Eve"),

age = c(25, 30, 35, 40, 45)

13. Function to Print First Two Rows of a Data Frame:


head(df, 2)

14. Date and Time in R:

R provides various ways to handle dates and times using Date, POSIXct, and POSIXlt classes.

Accessing Date Components:

Sys.Date() # Current date

format(Sys.Date(), "%Y") # Year

format(Sys.Date(), "%m") # Month

format(Sys.Date(), "%d") # Day

Accessing Time Components:

Sys.time() # Current time

format(Sys.time(), "%H:%M:%S") # Hour, minute, second

15. Importing and Exporting File Formats in R:

Importing:

read.csv("file.csv") # For CSV files

read.table("file.txt") # For text files

Exporting:

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

write.table(df, "output.txt")
.

You might also like