Dar Assignment
Dar Assignment
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.
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).
x <- 1:10
Output of 1:12:
1: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.
# Function body
return(result)
return(scan)
1:2 + 3:4
[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)
class(x)
typeof(x)
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.
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.
Data Frame: Can contain multiple data types across columns (e.g., numerical, characters, logical).
df <- data.frame(
R provides various ways to handle dates and times using Date, POSIXct, and POSIXlt classes.
Importing:
Exporting:
write.csv(df, "output.csv")
write.table(df, "output.txt")
.