YouTip LogoYouTip

R Vector

Vectors are the most basic data type in R. There are 6 types of atomic vectors: logical, integer, double, character, complex, and raw. ## Creating Vectors Below we create single-element vectors, which can be any of the 6 types above: ## Example # Character atomic vector print("tutorial"); # Double atomic vector print(12.5) # Integer atomic vector print(23L) # Logical atomic vector print(TRUE) # Complex atomic vector print(2+3i) # Raw atomic vector print(charToRaw('hello')) Executing the above code outputs: "tutorial" 12.5 23 TRUE 2+3i 68 65 6c 6c 6f Below we create multi-element vectors, using the colon : operator between numbers: ## Example # Create a series from 5 to 13 v <-5:13 print(v) # Create a series from 6.6 to 12.6 v <-6.6:12.6 print(v) # If the last element doesn't belong to the series, it's discarded v <-3.8:11.4 print(v) Executing the above code outputs: 5 6 7 8 9 10 11 12 13 6.6 7.6 8.6 9.6 10.6 11.6 12.6 3.8 4.8 5.8 6.8 7.8 8.8 9.8 10.8 We can also use the sequence operator **seq()** to create vectors. Below we create a vector from 5 to 9 with increment 0.4: print(seq(5, 9, by = 0.4)) Executing the above code outputs: 5.0 5.4 5.8 6.2 6.6 7.0 7.4 7.8 8.2 8.6 9.0 The c() function can convert non-character elements to character: ## Example # Numbers and logical values will be converted to character type s <-c('apple','red',5,TRUE) print(s) Executing the above code outputs: "apple" "red" "5" "TRUE" * * * ## Accessing Vector Elements To access vector elements, use square brackets []. Index values start from 1 (different from other programming languages). If the index is negative, the element at that position will be removed. You can also use TRUE, FALSE, or 0 and 1. ## Example # Access vector elements using index t<-c("Sun","Mon","Tue","Wed","Thurs","Fri","Sat") u <-t[c(2,3,6)] print(u) # Use logical index, TRUE means read, FALSE means don't read v <-t[c(TRUE,FALSE,FALSE,FALSE,FALSE,TRUE,FALSE)] print(v) # The second and fifth elements will be removed x <-t[c(-2,-5)] print(x) # Use 0/1 index, 1 means read, 0 means don't read y <-t[c(0,0,0,0,0,0,1)] print(y) Executing the above code outputs: "Mon" "Tue" "Fri" "Sun" "Fri" "Sun" "Tue" "Wed" "Fri" "Sat" "Sun" * * * ## Vector Operations We can perform addition, subtraction, multiplication, or division on two vectors of the same length. The result is also output as a vector. ## Example # Create two vectors v1 <-c(3,1,4,5,0,12) v2 <-c(5,11,9,8,1,22) # Addition add.result<- v1+v2 print(add.result) # Subtraction sub.result<- v1-v2 print(sub.result) # Multiplication multi.result<- v1*v2 print(multi.result) # Division divi.result<- v1/v2 print(divi.result) Executing the above code outputs: 8 12 13 13 1 34 -2 -10 -5 -3 -1 -10 15 11 36 40 0 264 0.60000000 0.09090909 0.44444444 0.62500000 0.00000000 0.54545455 ### Recycling Vectors If two vectors have different lengths, the shorter one will recycle its elements until it matches the length of the longer vector. ## Example v1 <-c(1,8,7,5,0,12) v2 <-c(5,6) # V2 becomes c(5,6,5,6,5,6) add.result<- v1+v2 print(add.result) sub.result<- v1-v2 print(sub.result) Executing the above code outputs: 6 14 12 11 5 18 -4 2 2 -1 -5 6 ### Vector Sorting We can use the sort() function to sort vectors: ## Example v <-c(2,11,6,5,0,21, -7, 111) # Sorting sort.result<-sort(v) print(sort.result) # Set decreasing parameter to TRUE for descending order, default is FALSE for ascending order revsort.result<-sort(v, decreasing = TRUE) print(revsort.result) # Sort character type v <-c("Tutorial","Google","Zhihu","Facebook") sort.result<-sort(v) print(sort.result) # Descending order revsort.result<-sort(v, decreasing = TRUE) print(revsort.result) Executing the above code outputs: -7 0 2 5 6 11 21 111 111 21 11 6 5 2 0 -7 "Facebook" "Google" "Tutorial" "Zhihu" "Zhihu" "Tutorial" "Google" "Facebook
← R MatrixR Functions β†’