Create a vector named num with four elements (2, 0, 4, 6).
#a.Display the third element of the vector.
n<-c(2, 0, 4, 6)
print(n[3])
## [1] 4
#b.Display the all elements of the vector except first element.
print(n[-1])
## [1] 0 4 6
#c.Count the number of elements in the vector.
print(length(n))
## [1] 4
Create a vector named q3 that add two numbers 3 and 5. After that, add 100 to this vector and display the output.
q3<-3+5
print(q3+100)
## [1] 108
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.
animal<-c("cat", "tiger", "lion", "elephant")
print(animal)
## [1] "cat" "tiger" "lion" "elephant"
print(animal<-append(animal, "monkey", after = 4))
## [1] "cat" "tiger" "lion" "elephant" "monkey"
print(animal<-append(animal, "cow", after = 5))
## [1] "cat" "tiger" "lion" "elephant" "monkey" "cow"
Create two vectors named n1 and n2 of integers type (any number) and of length 3. Then, add and multiply the two vectors.
n1<-100L
n2<-103L
print(n1+n2)
## [1] 203
print(n1*n2)
## [1] 10300
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.
x<-sample(1:10, 4, replace=FALSE)
print(sum(x))
## [1] 22
print(mean(x))
## [1] 5.5
print(min(x))
## [1] 1
print(max(x))
## [1] 10
#b.Append 3 values (11-20) to the vector x created. Display the sum, mean, minimum and the maximum of the vector.
print(x<-append(x, sample(11:20, 3, replace=FALSE), after = 4))
## [1] 1 2 10 9 18 12 13
print(sum(x))
## [1] 65
print(mean(x))
## [1] 9.285714
print(min(x))
## [1] 1
print(max(x))
## [1] 18
#c.Display the first two values and last two values of vector x.
print(x[1:2])
## [1] 1 2
print(x[6:7])
## [1] 12 13
#d.Assign the vector x in ascending order to s1, descending order to s2 and reverse order to s3.
print(s1<-sort(x))
## [1] 1 2 9 10 12 13 18
print(s2<-sort(x, decreasing = TRUE))
## [1] 18 13 12 10 9 2 1
print(s3<-rev(x))
## [1] 13 12 18 9 10 2 1
#e.Display the second highest value in vector x.
print(length(s2))
## [1] 7
print(s2[2])
## [1] 13
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:
#cat("Enter two number:", "\n")
#a<-as.numeric(readLines("stdin", 1))
#b<-as.numeric(readLines("stdin", 1))
#cat("12+23 =", a+b, "\n")