0% found this document useful (0 votes)
8 views4 pages

Assignment

The document provides a series of R commands for various data manipulation tasks, including creating vectors, performing arithmetic operations, and working with matrices and data frames. It covers operations such as accessing elements, calculating means, appending elements, and filtering data. Additionally, it includes commands for handling factors and arrays, showcasing a range of functions available in R.

Uploaded by

Mahi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views4 pages

Assignment

The document provides a series of R commands for various data manipulation tasks, including creating vectors, performing arithmetic operations, and working with matrices and data frames. It covers operations such as accessing elements, calculating means, appending elements, and filtering data. Additionally, it includes commands for handling factors and arrays, showcasing a range of functions available in R.

Uploaded by

Mahi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Here are the R commands for each of the tasks:

1. Create a numeric vector and access the 3rd element:

v <- 1:10
v[3]

2. Element-wise addition between two vectors:

v1 <- c(2, 4, 6)
v2 <- c(1, 3, 5)
v1 + v2

3. Find the mean and sum of a numeric vector:

v <- c(10, 20, 30, 40)


mean(v)
sum(v)

4. Create a character vector and append a new element:

fruits <- c("Apple", "Banana", "Cherry")


fruits <- c(fruits, "Date")

5. Check if a vector contains a specific value:

x <- c(2, 4, 6, 8)
5 %in% x

6. Create a 3x3 matrix filled row-wise:

matrix(1:9, nrow = 3, byrow = TRUE)

7. Multiply two matrices:

A <- matrix(c(1, 2, 3, 4, 5, 6), nrow = 2)


B <- matrix(c(7, 8, 9, 10, 11, 12), nrow = 2)
A %*% B

8. Extract an element from the matrix (2nd row, 3rd column):


mat <- matrix(1:12, nrow = 3, ncol = 4)
mat[2, 3]

9. Compute the transpose of a matrix:

M <- matrix(1:6, nrow = 2, ncol = 3)


t(M)

10. Element-wise multiplication of two matrices:

M1 <- matrix(c(1, 2, 3, 4), nrow = 2)


M2 <- matrix(c(5, 6, 7, 8), nrow = 2)
M1 * M2

11. Create a 3D array with two matrices:

arr <- array(1:8, dim = c(2, 2, 2))

12. Access the element from the 1st row, 2nd column, 2nd matrix of the array:

arr[1, 2, 2]

13. Add two 3D arrays:

A <- array(1:8, dim = c(2, 2, 2))


B <- array(9:16, dim = c(2, 2, 2))
A+B

14. Sum all elements in a 3D array:

sum(arr)

15. Create a factor and check its levels:

f <- factor(c("Low", "High", "Medium", "High", "Low"))


levels(f)

16. Change the levels of a factor:

levels(f) <- c("L", "H", "M")


17. Sort a factor by its levels:

f <- factor(c("Small", "Medium", "Large"), levels = c("Small", "Medium", "Large"))


sort(f)

18. Count the frequency of each level in a factor:

f <- factor(c("Yes", "No", "Yes", "No", "Yes"))


table(f)

19. Convert a factor into numeric:

f <- factor(c("Low", "Medium", "High"))


as.numeric(f)

20. Remove a data frame element from a list:

lst <- list(a = 1, b = data.frame(x = 1:3), c = 4)


lst$b <- NULL

21. Create a data frame:

df <- data.frame(Name = c("John", "Alice", "Bob"),


Age = c(25, 30, 22),
Gender = c("Male", "Female", "Male"))

22. Add a new column to the data frame:

df$Country <- c("USA", "UK", "Canada")

23. Filter rows where Age > 25:

subset(df, Age > 25)

24. Select only Name and Age columns:

df[, c("Name", "Age")]


25. Calculate the average (mean) of the Age column:

mean(df$Age)

You might also like