# ------------------------------------------------------------------ # File name: vectors.R # # R is efficient in computing with vector variables # By default, a variable holding a single number is a vector of length 1 # Below is an annotated summary of common operations with vector variables # # Version: 2.1 # Authors: H. Kocak, University of Miami and B. Koc, Stetson University # References: # https://www.r-project.org # ------------------------------------------------------------------ # R function c() combines (concatanes) values into a vector x = c(1.5, -3.2, 0.45, 4.1, 10) print(x) # Entry of a vector can be addressed by its index, its position in the vector # Unlike other programming languages, index of a vector in R starts from 1 # To print the first entry of x print(x[1]) # To print the 4th entry of x print(x[4]) # To change the value of the 4th entry of x to 7.6 x[4] = 7.6 print(x) # Can add an entry to a vector x[6] = 91 print(x) # An entry of a vector can be deleted using -index x = x[-6] print (x) # R function length() returns the number of entries of a vector # There are numerous function for vectors; try, for example # sort(), rev(), min(), mean(), sum(), prod() length_of_x = length(x) print(length_of_x) # Arithmetic of vectors is done entry-wise # + addition, - subtraction, * product, / division, %*% crossproduct vector_1 = c(1.1, 2.3, 4.5) vector_2 = c(-1, 1.2, 0.5) vector_sum = vector_1 + vector_2 print(vector_sum) # Vector entries can be strings or booleans nucleotides = c("A", "C", "G", "T") print(nucleotides) # Vectors with patterns are useful. We will see a better way to generate them x = c(0, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0) print (x) squareRoot_x = sqrt(x) print(squareRoot_x)