Lab 1 Getting to know R

Group 7

WQD7004 Programming for Data Science Lab 1 Getting to know R

Question 1

  1. Installation of R (https://cran.rstudio.com/) and RStudio (https://www.rstudio.com/products/rstudio/download/)

Question 2

Create a vector named num with four elements (2, 0, 4, 6). a. Display the third element of the vector. b. Display the all elements of the vector except first element. c. Count the number of elements in the vector.

output: 
  > v1 <- c(2,0,4,6)
  > v1
    [1] 2 0 4 6
  > v1[3]
    [1] 4
  > v1[2:4]
    [1] 0 4 6
  > length(v1)
    [1] 4

Question 3

Create a vector named q3 that add two numbers 3 and 5. After that, add 100 to this vector and display the output.

output: 
  > q3 <- c(3,5)
  > q3
    [1] 3 5
  > q3+100
    [1] 103 105

Question 4

Create a vector named animal that consists of cat, tiger, lion and elephant. Display the vector. After that, append monkey and cow to the vector and display the output.

output: 
  > animal = c('cat','tiger','lion','elephant')
  > animal <- append(animal, 'monkey')
  > animal <- append(animal, 'cow')
  > print(animal)
    [1] "cat"      "tiger"    "lion"     "elephant" "monkey"   "cow"     

Question 5

Create two vectors named n1 and n2 of integers type (any number) and of length 3. Then, add and multiply the two vectors.

output: 
  > n1 <- c(1L,2L,3L)
  > n2 <- c(4L,5L,6L)
  > n1+n1
    [1] 2 4 6
  > n1*n2
    [1]  4 10 18

Question 6

Create a vector x of size 4 with any value from 1-10. a. Display the sum, mean, minimum and the maximum of the vector x. b. Append 3 values (11-20) to the vector x created. Display the sum, mean, minimum and the maximum of the vector. c. Display the first two values and last two values of vector x. d. Assign the vector x in ascending order to s1, descending order to s2 and reverse order to s3. e. Display the second highest value in vector x. *mean in two decimal places.

output: 
  a:
  x <- c(2,4,6,8)
  
  b:
  > sum(x)
    [1] 20
  > min(x)
    [1] 2
  > max(x)
    [1] 8
  > mean(x)
    [1] 5
  > median(x)
    [1] 5
  > mode(x)
    [1] "numeric"
  > sd(x)
    [1] 2.581989
    
    > x <- append(x,11:13,after = 4)
    > x
      [1]  2  4  6  8 11 12 13
    > sum(x)
      [1] 56
    > min(x)
      [1] 2
    > max(x)
      [1] 13
    > mean(x)
      [1] 8
    > round(mean(x),2)
      [1] 15.72
      
      c:
      > x[1:2]
        [1] 2 4
      > head(x, n= 2)
        [1] 2 4
      > tail(x, n=2)
        [1] 12 13
      
      d:
      > sort(x, decreasing = FALSE)
        [1]  2  4  6  8 11 12 13
      > sort(x, decreasing = TRUE)
        [1] 13 12 11  8  6  4  2
      > s1 <- order(x)
      > s1
        [1] 1 2 3 4 5 6 7
      > s2 <- order(-x)
      > s2
        [1] 7 6 5 4 3 2 1
      > s3 <- rev(x)
      > s3
        [1] 13 12 11  8  6  4  2
      
      e:
      > sort(x) [length(x) -1]
        [1] 12
      > sort(x) [2]
        [1] 4
    

Question 7:

Create an R file named total.r that get two integer input from user. Display the total of the input. Run the r file using terminal. Example output:
Enter two number:
12
23
[1] “12 + 23 = 35”

output: 
  > num_1 <- readline(prompt = "Enter your numbers:")
    Enter your numbers:12
  > num_2 <- readline(prompt = "Enter second numbers:")
    Enter second numbers:23
  > num_1 <- as.integer(num_1)
  > num_2 <- as.integer(num_2)
  > print(paste("The total number is:",num_1+num_2))
    [1] "The total number is: 35"