exp1

exp1

#Vector Creation > #Single Element Vector > print(“ABC”); [1] “ABC” > print(TRUE): + + print(TRUE) [1] TRUE [1] TRUE [1] 1 > print(charToRaw(‘hello’)) [1] 68 65 6c 6c 6f > #————————————————————— > #Multiple Element Vector > v <- 5:13 > > print(v) [1] 5 6 7 8 9 10 11 12 13 > v <- 6.6:12.6 > print(v) [1] 6.6 7.6 8.6 9.6 10.6 11.6 12.6 > #————————————————————— > #Using the c() function > s <- c(‘apple’,’red’,5,TRUE) > print(s) [1] “apple” “red” “5” “TRUE” > #—————————————————————————- > #Accessing Vector elements > t <- c(“Sun”,”Mon”,”Tue”,”Wed”,”Thurs”,”Fri”,”Sat”) > u <- t[c(2,3,6)] > print(u) [1] “Mon” “Tue” “Fri” > y <- t[c(0,0,0,0,0,0,1)] > print(y) [1] “Sun” > #————————————————————————- > #vector Manipulation > #Vector arithmetic > # Create two vectors. > v1 <- c(3,8,4,5,0,11) > v2 <- c(4,11,0,8,1,2) > > # Vector addition. > add.result <- v1+v2 > print(add.result) [1] 7 19 4 13 1 13 > # Vector subtraction. > sub.result <- v1-v2 > print(sub.result) [1] -1 -3 4 -3 -1 9 R Console Page 2 > #————————————————————————- > #Vector Element Recycling > v1 <- c(3,8,4,5,0,11) > v2 <- c(4,11) > # V2 becomes c(4,11,4,11,4,11) > > add.result <- v1+v2 > print(add.result) [1] 7 19 8 16 4 22 > #——————————————————————————– > Vector Element Sorting Error: unexpected symbol in “Vector Element” > #Vector Element Sorting > v <- c(3,8,4,5,0,11, -9, 304) > # Sort the elements of the vector. > sort.result <- sort(v) > print(sort.result) [1] -9 0 3 4 5 8 11 304 > # Sort the elements in the reverse order. > revsort.result <- sort(v, decreasing = TRUE) > print(revsort.result) [1] 304 11 8 5 4 3 0 -9 > #————————————————

Scroll to Top