0% found this document useful (0 votes)
23 views10 pages

Vec

Uploaded by

e25debleenar
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)
23 views10 pages

Vec

Uploaded by

e25debleenar
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

> vec <- c(2, 4, 6)

> sum(vec)

[1] 12

> sum(vec1 * vec2)

Error: object 'vec1' not found

> vec1=c(2,3,7)

> sum(vec * vec1)

[1] 58

> sum(vec^2)

[1] 56

> head(vec,1)

[1] 2

> head(vec,3)

[1] 2 4 6

> head(vec,4)

[1] 2 4 6

> head(vec,2)

[1] 2 4

> pdt=(Vec*vec1)

Error: object 'Vec' not found

> pdt=(vec*vec1)

> print(pdt)

[1] 4 12 42

> for(i in 1:l)

+ {pdt[i]=vec[i]*vec1[i]}

Error: object 'l' not found

> for(i in 1:3)

+ {pdt[i]=vec[i]*vec1[i]}

> print(pdt)
[1] 4 12 42

> for(i in 1:3)

+ + {sum1[i]=vec[i]*vec1[i]}

Error: object 'sum1' not found

> sum1=mumeric(3)for(i in 1:3)

Error: unexpected 'for' in "sum1=mumeric(3)for"

> sum1=mumeric(3)

Error in mumeric(3) : could not find function "mumeric"

> sum1=mumeric(len(vec))

Error in mumeric(len(vec)) : could not find function "mumeric"

> sum1=numeric(len(vec))

Error in len(vec) : could not find function "len"

> sum1=numeric(length(vec))

> for(i in 1:3) {sum1[i]=vec[i]*vec1[i]}

> print(sum1)

[1] 4 12 42

> sum1=numeric(length(vec))

> for(i in 1:3) {sum1[i]=vec[i]+vec1[i]}

> print(sum1)

[1] 4 7 13

> mat <- matrix(1:9, nrow = 3, byrow = TRUE)

> print(mat)

[,1] [,2] [,3]

[1,] 1 2 3

[2,] 4 5 6

[3,] 7 8 9

>

> # 2. Extract the second row

> second_row <- mat[2, ]


> print(second_row)

[1] 4 5 6

>

> # 3. Compute transpose of the matrix

> mat_transpose <- t(mat)

> print(mat_transpose)

[,1] [,2] [,3]

[1,] 1 4 7

[2,] 2 5 8

[3,] 3 6 9

>

> # 4. Matrix multiplication (true matrix multiplication)

> mat2 <- matrix(seq(1, 9), nrow = 3, byrow = FALSE)

> mat_mult <- mat %*% mat2

> print(mat_mult)

[,1] [,2] [,3]

[1,] 14 32 50

[2,] 32 77 122

[3,] 50 122 194

>

> # 5. Sum of all elements

> sum_mat <- sum(mat)

> print(sum_mat)

[1] 45

> mat <- matrix(1:9, ncol = 3, byrow = TRUE)

> print(mat)

[,1] [,2] [,3]

[1,] 1 2 3

[2,] 4 5 6
[3,] 7 8 9

> mat <- matrix(1:9, ncol = 3, byrow = FALSE)

> print(mat)

[,1] [,2] [,3]

[1,] 1 4 7

[2,] 2 5 8

[3,] 3 6 9

> mat <- matrix(1:9, nrow=2, ncol = 3, byrow = FALSE)

Warning message:

In matrix(1:9, nrow = 2, ncol = 3, byrow = FALSE) :

data length [9] is not a sub-multiple or multiple of the number of rows [2]

> print(mat)

[,1] [,2] [,3]

[1,] 1 3 5

[2,] 2 4 6

> mat <- matrix(1:9, nrow=4, ncol = 3, byrow = FALSE)

Warning message:

In matrix(1:9, nrow = 4, ncol = 3, byrow = FALSE) :

data length [9] is not a sub-multiple or multiple of the number of rows [4]

> print(mat)

[,1] [,2] [,3]

[1,] 1 5 9

[2,] 2 6 1

[3,] 3 7 2

[4,] 4 8 3

> mat <- matrix(1:12, nrow=4, ncol = 3, byrow = FALSE)

> print(mat)

[,1] [,2] [,3]

[1,] 1 5 9
[2,] 2 6 10

[3,] 3 7 11

[4,] 4 8 12

> second_col <- mat[,2]

> print(second_col)

[1] 5 6 7 8

> second_col <- mat[,2:3]

> print(second_col)

[,1] [,2]

[1,] 5 9

[2,] 6 10

[3,] 7 11

[4,] 8 12

> print(mat2)

[,1] [,2] [,3]

[1,] 1 4 7

[2,] 2 5 8

[3,] 3 6 9

> mat_mult <- mat %*% mat2

> print(mat_mult)

[,1] [,2] [,3]

[1,] 38 83 128

[2,] 44 98 152

[3,] 50 113 176

[4,] 56 128 200

> # 1. Create a list containing name, age, and vector of three favorite
numbers

> my_list <- list(name = "John", age = 25, fav_numbers = c(3, 7, 11))

> print(my_list)
$name

[1] "John"

$age

[1] 25

$fav_numbers

[1] 3 7 11

>

> # 2. Access the second element of a list

> second_element <- my_list[[2]]

> print(second_element)

[1] 25

>

> # 3. Modify an element of an existing list (change age to 26)

> my_list$age <- 26

> print(my_list)

$name

[1] "John"

$age

[1] 26

$fav_numbers

[1] 3 7 11

>

> # 4. Add a new element to a list


> my_list$city <- "New York"

> print(my_list)

$name

[1] "John"

$age

[1] 26

$fav_numbers

[1] 3 7 11

$city

[1] "New York"

> my_list[1]

$name

[1] "John"

> my_list[[1]]

[1] "John"

> my_list[[1:3]]

Error in my_list[[1:3]] : recursive indexing failed at level 2

> my_list[1:3]

$name

[1] "John"

$age

[1] 26
$fav_numbers

[1] 3 7 11

> my_list

$name

[1] "John"

$age

[1] 26

$fav_numbers

[1] 3 7 11

$city

[1] "New York"

> # 1. Create data frame with names, ages, salaries

> employees <- data.frame(

+ Name = c("John", "Alice", "Bob", "Diana", "Charlie"),

+ Age = c(28, 32, 25, 29, 35),

+ Salary = c(50000, 60000, 45000, 52000, 70000)

+)

> print(employees)

Name Age Salary

1 John 28 50000

2 Alice 32 60000

3 Bob 25 45000

4 Diana 29 52000

5 Charlie 35 70000
>

> # 2. Extract "Name" column

> names_column <- employees$Name

> print(names_column)

[1] "John" "Alice" "Bob" "Diana" "Charlie"

>

> # 3. Add a new row

> new_row <- data.frame(Name = "Eva", Age = 30, Salary = 65000)

> employees <- rbind(employees, new_row)

> print(employees)

Name Age Salary

1 John 28 50000

2 Alice 32 60000

3 Bob 25 45000

4 Diana 29 52000

5 Charlie 35 70000

6 Eva 30 65000

>

> # 4. Sort based on "Salary"

> employees_sorted <- employees[order(employees$Salary), ]

> print(employees_sorted)

Name Age Salary

3 Bob 25 45000

1 John 28 50000

4 Diana 29 52000

2 Alice 32 60000

6 Eva 30 65000

5 Charlie 35 70000

>
> # 5. Filter salary > 55000

> high_salary <- subset(employees, Salary > 55000)

> print(high_salary)

Name Age Salary

2 Alice 32 60000

5 Charlie 35 70000

6 Eva 30 65000

You might also like