Thursday, March 25, 2021

R LANGUAGE SOLVES PRACTICAL SLIPS

           

    

1]Write an R program to find the maximum and the minimum value of a given vector. 

Answer:

> n= c(10, 20, 30, 40, 50, 60)

> print(n)

[1] 10 20 30 40 50 60                                                      

> print(paste("Maximum number:",max(n)))

[1] "Maximum number: 60"

> print(paste(":minimum number:",min(n)))

[1] ":minimum number: 10"    

 

2] Write an R program to sort a Vector in ascending and descending order.

Answer:

  > x = c(10, 20, 30, 25, 9, 26)

> print(x)

[1] 10 20 30 25  9 26

> print("Sort in ascending order:")

[1] "Sort in ascending order:"

> print(sort(x))

[1]  9 10 20 25 26 30

> print("Sort in descending order:")

[1] "Sort in descending order:"a

> print(sort(x, decreasing=TRUE))

[1] 30 26 25 20 10  9

 

3]Write an R program to compare two data frames to find the elements in first data frame that are not present in second data frame.

 

Answer:

> a = c("a", "b", "c", "d", "e")

> b = c("d", "e", "f", "g")

> print("Original frame")

[1] "Original frame"

> print(a)

[1] "a" "b" "c" "d" "e"

> print(b)

[1] "d" "e" "f" "g"

> print("Data in first dataframe  that are not present in second dataframe:")

[1] "Data in first dataframe  that are not present in second dataframe:"

> result = setdiff(a, b)

> print(result)

[1] "a" "b" "c"

 

4]Write an R program to extract first 10 English letter in lower case and last 10 letters in upper case and extract letters between 22nd to 24th letters in upper case.

Answer:

> print("First 10 letters in lower case:")

[1] "First 10 letters in lower case:"

> t = head(letters, 10)

> print(t)

 [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j"

> print("Last 10 letters in upper case:")

[1] "Last 10 letters in upper case:"

> t = tail(LETTERS, 10)

> print(t)

 [1] "Q" "R" "S" "T" "U" "V" "W" "X" "Y" "Z"

> print("Letters between 22nd to 24th letters in upper case:")

[1] "Letters between 22nd to 24th letters in upper case:"

> e = tail(LETTERS[22:24])

> print(e)

[1] "V" "W" "X"

 

5] Write an R program to find Sum, Mean and Product of a Vector.              

Answer:

> x = c(23,32,46)

> print("Sum:")

[1] "Sum:"

> print(sum(x))

[1] 101

> print("Mean:")

[1] "Mean:"

> print(mean(x)) 

[1] 33.66667

> print("Product:")

[1] "Product:"

> print(prod(x))

[1] 33856

 

6] Write an R program to create a simple bar plot of five subject’s marks

Answer:

marks = c(70, 95, 80, 74)

barplot(marks,

main = "Comparing marks of 5 subjects",

xlab = "Marks",

ylab = "Subject",

names.arg = c("English", "Science", "Math.", "Hist."),

col = "lightpink",

)

 

7] Write an R program to create a Dataframes which contain details of 5 employees and display the details in ascending order

Answer:

 

8] Write an R program to create a data frame using two given vectors and display the duplicated elements and unique rows of the said data frame

Answer: > a = c(10,20,10,10,40,50,20,30)

> b = c(10,30,10,20,0,50,30,30)

> print("Original data frame:")

[1] "Original data frame:"

> ab = data.frame(a,b)

> print(ab)

   a  b

1 10 10

2 20 30

3 10 10

4 10 20

5 40  0

6 50 50

7 20 30

8 30 30

> print("Duplicate elements of the said data frame:")

[1] "Duplicate elements of the said data frame:"

> print(duplicated(ab))

[1] FALSE FALSE  TRUE FALSE FALSE FALSE  TRUE FALSE

> print("Unique rows of the said data frame:")

[1] "Unique rows of the said data frame:"

> print(unique(ab))

   a  b

1 10 10

2 20 30

4 10 20

5 40  0

6 50 50

8 30 30

 

9] Write an R program to change the first level of a factor with another level of a given factor

Answer:

> v = c("a", "b", "a", "c", "b")

> print("Original vector:")

[1] "Original vector:"

> print(v)

[1] "a" "b" "a" "c" "b"

> f = factor(v)

> print("Factor of the said vector:")

[1] "Factor of the said vector:"

> print(f)

[1] a b a c b

Levels: a b c

> levels(f)[1] = "e"

> print(f)

[1] e b e c b

Levels: e b c

> levels(f)[1] = "f"

> print(f)

[1] f b f c b

Levels: f b c

10]    Write a script in R to create a list of cities and perform the following

1) Give names to the elements in the list.

2) Add an element at the end of the list.

3) Remove the last element.

4) Update the 3rd Element

 

Answer:

> list_data <- list("India","china","usk")

> print(list_data)

[[1]]

[1] "India"

[[2]]

[1] "china"

[[3]]

[1] "usk"

> names(list_data) <- c("1st Quarter", "A_Matrix", "A Inner list")

> print(list_data)

$`1st Quarter`

[1] "India"

 

$A_Matrix

[1] "china"

 

$`A Inner list`

[1] "usk"

 

> list_data[4] <- "New element"

> print(list_data[4])

[[1]]

[1] "New element"

 

> list_data[4] <- NULL

> print(list_data[4])

$<NA>

NULL

 

> list_data[3] <- "updated element"

> print(list_data[3])

$`A Inner list`

[1] "updated element"


11] Write a script in R to create two vectors of different lengths and give these vectors as input to array and print addition and subtraction of those matrices.

Answer:

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

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

> array1 <- array(c(vector1, vector2), dim = c(3, 3, 2))

> vector3 <- c(9, 1, 0)

> vector4 <- c(6, 0, 11, 3, 14, 1, 2, 6, 9)

> array2 <- array(c(vector1, vector2), dim = c(3, 3, 2))

> matrix1 <- array1[,,2]

> matrix2 <- array2[,,2]

> result <- matrix1 + matrix2

> print(result)

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

[1,]   10   20   26

[2,]   18   22   28

[3,]    6   24   30

> result <- matrix1 - matrix2

> print(result)

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

[1,]    0    0    0

[2,]    0    0    0

[3,]    0    0    0

 

>v1<-c(2,3,6,7)

> v2<-c(5,1,4,9,8)

> array1<-array(c(v1,v2),dim=c(3,3,2))

> v3<-c(11,23,14,15)

> v4<-c(23,8,23,14,13)

> array2<-array(c(v3,v4),dim=c(3,3,2))

> matrix1<-array1[,,2]

> matrix2<-array2[,,2]

> result=matrix1+matrix2

> print(result)

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

[1,]   13   22   27

[2,]   26   28   23

[3,]   20    9   21

> result=matrix1-matrix2

> print(result)

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

[1,]   -9   -8  -19

[2,]  -20  -18   -5

[3,]   -8   -7   -5

> result=matrix1*matrix2

> print(result)

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

[1,]   22  105   92

[2,]   69  115  126

[3,]   84    8  104

> result=matrix1/matrix2

> print(result)

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

[1,] 0.1818182 0.4666667 0.1739130

[2,] 0.1304348 0.2173913 0.6428571

[3,] 0.4285714 0.1250000 0.6153846

> result=matrix1%matrix2

Error: unexpected input in "result=matrix1%matrix2"

> result=matrix1&matrix2

> print(result)

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

[1,] TRUE TRUE TRUE

[2,] TRUE TRUE TRUE

[3,] TRUE TRUE TRUE

> result=matrix1&&matrix2

> print(result)

[1] TRUE

> result=matrix1%%matrix2

> print(result)

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

[1,]    2    7    4

[2,]    3    5    9

[3,]    6    1    8

> result=matrix1==matrix2

> print(result)

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

[1,] FALSE FALSE FALSE

[2,] FALSE FALSE FALSE

[3,] FALSE FALSE FALSE

 

12] Write an R Program to calculate Multiplication Table

Answer:

> a<-23

> for(i in 1:10){

+ print(paste(a,"x",i,"=",a*i))

+ }

[1] "23 x 1 = 23"

[1] "23 x 2 = 46"

[1] "23 x 3 = 69"

[1] "23 x 4 = 92"

[1] "23 x 5 = 115"

[1] "23 x 6 = 138"

[1] "23 x 7 = 161"

[1] "23 x 8 = 184"

[1] "23 x 9 = 207"

[1] "23 x 10 = 230"

 

13] Write an R program to concatenate two given factor in a single factor and display in descending order.            

Answer:

> f1 <- factor(sample(LETTERS, size=6, replace=TRUE))

> f2 <- factor(sample(LETTERS, size=6, replace=TRUE))

> print("Original factors:")

[1] "Original factors:"

> print(f1)

[1] N J T F A X

Levels: A F J N T X

> print(f2)

[1] L P R Q U P

Levels: L P Q R U

> f = factor(c(levels(f1)[f1], levels(f2)[f2]))

> print("After concatenate factor becomes:")

[1] "After concatenate factor becomes:"

> print(f)

 [1] N J T F A X L P R Q U P

Levels: A F J L N P Q R T U X

                               

14]Write an R program to extract the five of the levels of factor created from a random sample from the LETTERS

Answer:

> L = sample(LETTERS,size=50,replace=TRUE)

> print("Original data:")

[1] "Original data:"

> print(L)

 [1] "Z" "L" "E" "F" "Y" "T" "J" "G" "U" "G" "O" "T" "V" "T" "I" "C" "A" "O" "V"

[20] "K" "B" "C" "W" "P" "F" "W" "J" "D" "T" "B" "B" "N" "H" "X" "G" "Q" "U" "X"

[39] "O" "A" "Q" "R" "Q" "L" "M" "D" "L" "Q" "S" "O"

> f = factor(L)

> print("Original factors:")

[1] "Original factors:"

> print(f)

 [1] Z L E F Y T J G U G O T V T I C A O V K B C W P F W J D T B B N H X G Q U X

[39] O A Q R Q L M D L Q S O

Levels: A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

> print("Only five of the levels")

[1] "Only five of the levels"

> print(table(L[1:5]))

 

E F L Y Z

1 1 1 1 1

15] Write an R Program to calculate Decimal into binary of a given number.     

Answer:

> db<- function(n) {

+ if(n > 1) {

+ db(as.integer(n/2))

+ }

+ cat(n %% 2)

+ }

> db(5)

101>

16)Write an R program to create three vectors a,b,c with 3 integers. Combine the three vectors to become a 3×3 matrix where each column represents a vector. Print the content of the matrix.    

> a<-c(1,2,3)

> b<-c(4,5,6)

> c<-c(7,8,9)

> m<-cbind(a,b,c)

> print("Content of the said matrix:")

[1] "Content of the said matrix:"

> print(m)

     a b c

[1,] 1 4 7

[2,] 2 5 8

[3,] 3 6 9

17] Write an R program to draw an empty plot and an empty plot specify the axes limits of the graphic

Answer:

> plot.new()

> #print("Empty plot specify the axes limits of the graphic:")

> plot(1, type="n", xlab="", ylab="", xlim=c(0, 20), ylim=c(0, 20))

 

18] Write an R program to print the numbers from 1 to 100 and print "SY" for multiples of 3, print "BBA" for multiples of 5, and print "SYBBA" for multiples of both. 

Answer:

                           > print("number between 1 to 100")

[1] "number between 1 to 100"

> for (n in 1:100) {

+  if (n %% 3 == 0 & n %% 5 == 0) {print("SYBBA")}

+  else if (n %% 3 == 0) {print("SY")}

+  else if (n %% 5 == 0) {print("BBA")}

+  else print(n)

+ }

[1] 1

[1] 2

[1] "SY"

[1] 4

[1] "BBA"

[1] "SY"

[1] 7

[1] 8

[1] "SY"

[1] "BBA"

[1] 11

[1] "SY"

[1] 13

[1] 14

[1] "SYBBA"

[1] 16

[1] 17

[1] "SY"

[1] 19

[1] "BBA"

[1] "SY"

[1] 22

[1] 23

[1] "SY"

[1] "BBA"

[1] 26

[1] "SY"

[1] 28

[1] 29

[1] "SYBBA"

[1] 31

[1] 32

[1] "SY"

[1] 34

[1] "BBA"

[1] "SY"

[1] 37

[1] 38

[1] "SY"

[1] "BBA"

[1] 41

[1] "SY"

[1] 43

[1] 44

[1] "SYBBA"

[1] 46

[1] 47

[1] "SY"

[1] 49

[1] "BBA"

[1] "SY"

[1] 52

[1] 53

[1] "SY"

[1] "BBA"

[1] 56

[1] "SY"

[1] 58

[1] 59

[1] "SYBBA"

[1] 61

[1] 62

[1] "SY"

[1] 64

[1] "BBA"

[1] "SY"

[1] 67

[1] 68

[1] "SY"

[1] "BBA"

[1] 71

[1] "SY"

[1] 73

[1] 74

[1] "SYBBA"

[1] 76

[1] 77

[1] "SY"

[1] 79

[1] "BBA"

[1] "SY"

[1] 82

[1] 83

[1] "SY"

[1] "BBA"

[1] 86

[1] "SY"

[1] 88

[1] 89

[1] "SYBBA"

[1] 91

[1] 92

[1] "SY"

[1] 94

[1] "BBA"

[1] "SY"

[1] 97

[1] 98

[1] "SY"

[1] "BBA"

 

19)Write a script in R to create two vectors of different lengths and give these vectors as input to array and print second row of second matrix of the array.     

Answer:

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

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

> result <- array(c(v1,v2),dim = c(3,3,2))

> print(result)

, , 1

 

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

[1,]    5   10   13

[2,]    9   11   14

[3,]    3   12   15

 

, , 2

 

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

[1,]    5   10   13

[2,]    9   11   14

[3,]    3   12   15

 

> print(result[2,,2])

[1]  9 11 14

 

20)Write a script in R to create two vectors of different lengths and give these vectors as input to array and print Multiplication of those matrices

Answer:

> v1<-c(2,4,6,7)

> v2<-c(3,5,8,9,11,24)

> array1<-array(c(v1,v2),dim=c(3,3,2))

> v3<-c(31,11,12,14)

> v4<-c(27,22,29,19,41)

> array2<-array(c(v3,v4),dim=c(3,3,2))

> matrix1<-array1[,,2]

> matrix2<-array2[,,2]

> result=matrix1+matrix2

> print(result)

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

[1,]   55   20   34

[2,]   13   34   27

[3,]   16   25   50

 

21) Write an R program to create a list of elements using vectors, matrices and a functions. Print the content of the list.

answer:

> l = list(

+   c(1, 2, 2, 5, 7, 12), 

+   month.abb,

+   matrix(c(1,3,2,4), nrow = 2),

+   max

+ )

> print("Content of the list:")

[1] "Content of the list:"

> print(l)

[[1]]

[1]  1  2  2  5  7 12

 

[[2]]

 [1] "Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug" "Sep" "Oct" "Nov" "Dec"

 

[[3]]

     [,1] [,2]

[1,]    1    2

[2,]    3    4

 

[[4]]

function (..., na.rm = FALSE)  .Primitive("max")

 

22) Write a script in R to create an array, passing in a vector of values and a vector of dimensions. Also provide names for each dimension

Answer:

> a =  array(

+   6:30,

+   dim = c(4, 3, 2),

+   dimnames = list(

+     c("C1", "C2", "C3", "C4"),

+     c("R1", "R2", "R3"),

+     c("P1", "P2")

+   )

+ )

> print(a)

, , P1

 

   R1 R2 R3

C1  6 10 14

C2  7 11 15

C3  8 12 16

C4  9 13 17

 

, , P2

 

   R1 R2 R3

C1 18 22 26

C2 19 23 27

C3 20 24 28

C4 21 25 29

 

23)Write an R Program to calculate binary into Decimal of a given number

Answer:

 

24) Write an R program to convert a given matrix to a list and print list in ascending order

Answer:

m = matrix(1:10,nrow=2, ncol=2)

print("Original matrix:")

[1] "Original matrix:"

 print(m)

     [,1] [,2]

[1,]    1    3

[2,]    2    4

> as.list(as.data.frame(m))

$V1

[1] 1 2

 

$V2

[1] 3 4

 

> print("list from the said matrix:")

[1] "list from the said matrix:"

> print(l)

$`1`

[1] 1 2

 

$`2`

[1] 3 4

 

> print(sort(m))

[1] 1 2 3 4

 

 

 

 



No comments:

Post a Comment