0% found this document useful (0 votes)
26 views6 pages

Part-A: - Create Vector For The Following Using Following Numbers

The document provides examples of R code for creating vectors, finding mean, mode and sum of vectors, constructing matrices, and merging dataframes. It includes code for creating vectors, accessing vector elements, calculating statistics, constructing matrices with random and predefined values, and modifying matrix elements.

Uploaded by

Aarti Rethadiya
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)
26 views6 pages

Part-A: - Create Vector For The Following Using Following Numbers

The document provides examples of R code for creating vectors, finding mean, mode and sum of vectors, constructing matrices, and merging dataframes. It includes code for creating vectors, accessing vector elements, calculating statistics, constructing matrices with random and predefined values, and modifying matrix elements.

Uploaded by

Aarti Rethadiya
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/ 6

PART-A

Q1. Create vector for the following using following numbers


> v <-c(12,7,3,4.2,18,2,54,-21,8,-5)

>v

[1] 12.0 7.0 3.0 4.2 18.0 2.0 54.0 -21.0 8.0 -5.0

Store in to the variables:

> my_vector <-c(12,7,3,4.2,18,2,54,-21,8,-5)

> x <- my_vector[18.0]

> y <- sum(my_vector)

>x

[1] NA

Find mean values:

> v <-c(12,7,3,4.2,18,2,54,-21,8,-5)

> result.mean <-mean(v)

> print(result.mean)

[1] 8.22

Store mean value in variable :

> my_vector <-c(12,7,3,4.2,18,2,54,-21,8,-5)

> x <- my_vector[8.22]

> y <- sum(my_vector)

>x

[1] -21

Print mean value

> v <-c(12,7,3,4.2,18,2,54,-21,8,-5)

> print <- my_vector[8.22]

> print

[1] -21

Q2. .Create vector for the following using following numbers


> v <-c(2,1,2,3,1,2,3,4,1,5,5,3,2,3)

>v

[1] 2 1 2 3 1 2 3 4 1 5 5 3 2 3

Store in to the variable

> my_vector <-c(2,1,2,3,1,2,3,4,1,5,5,3,2,3)

> x <-my_vector[8]

> y <-sum(my_vector)

>x

[1] 4

Find mode value

> v <-c(2,1,2,3,1,2,3,4,1,5,5,3,2,3)

> result.mode <-mode(v)

> print(result.mode)

[1] "numeric"

Store mode value in variable

> x <-my_vector[4]

> y <-sum(my_vector)

>x

[1] 3

Print mode value

> v <-c(2,1,2,3,1,2,3,4,1,5,5,3,2,3)

> print <-my_vector[3]

> print

[1] 2

Q3.Construct matrix
> m <- matrix(1:10, nrow=5, ncol=4, byrow=TRUE)

>m

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

[1,] 1 2 3 4

[2,] 5 6 7 8

[3,] 9 10 1 2

[4,] 3 4 5 6

[5,] 7 8 9 10

IF required use vectore.

Print dimensions of the matrix

> m <- matrix(1:10, nrow=5, ncol=4, byrow=TRUE)

>m

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

[1,] 1 2 3 4

[2,] 5 6 7 8

[3,] 9 10 1 2

[4,] 3 4 5 6

[5,] 7 8 9 10

> m <- matrix(0, nrow=4, ncol=4)

>m

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

[1,] 0 0 0 0

[2,] 0 0 0 0

[3,] 0 0 0 0

[4,] 0 0 0 0

> m <- matrix(runif(6), nrow=2, ncol=3)

>m
[,1] [,2] [,3]

[1,] 0.2624571 0.65967975 0.2207184

[2,] 0.5837729 0.00933649 0.2040405

> v <-c(2,1,2,3,1,2,3,4,1,5,5,3,2,3)

> m <- matrix(v, nrow=2, ncol=7)

>m

[,1] [,2] [,3] [,4] [,5] [,6] [,7]

[1,] 2 2 1 3 1 5 2

[2,] 1 3 2 4 5 3 3

> m <- matrix(1:10, nrow=5, ncol=4, byrow=TRUE)

> m[2, 2] <- 0

> m[, 2] <- c(5, 4, 7)

> m <- matrix(1:10, nrow=5, ncol=4, byrow=TRUE)

> m[2, 2] <- 0

> m[2, 2] <- c(5, 4, 7)

> m <-m[-3,]

>m

[,1] [,2] [,3] [,4] [,5]

[1,] 1 2 3 4 7

[2,] 5 0 7 8 8

[3,] 3 4 5 6 7

[4,] 7 8 9 10 8

Print matrix

> v <-c(2,1,2,3,1,2,3,4,1,5,5,3,2,3)

> print <-my_vector[10]

> print

[1] 5

Q4.
Create two vectors of different lengths
Vector 1.

> v <-c(5,9,3)

>v

[1] 5 9 3

Vector 2.

> v <-c(10,11,12,13,14,15)

>v

[1] 10 11 12 13 14 15

Print the variable

> my_vector <-c(5,9,3)

> my_vector[3]

[1] 3

> print(my_vector)

[1] 5 9 3

> my_vector <-c(5,9,3)

> x <- my_vector[3]

> y <-sum(my_vector)

>x

[1] 3

>y

[1] 17

PART B
Q1
> merged_df <- merge(df, df, by = "key")

> merged_df

key value.x value.y

1 book 2.5 2.5

2 pen 10.0 10.0

3 pencil 7.0 7.0

4 text 8.0 8.0

You might also like