2.6 Vectorized Operations
2.6.1 Vector In, Vector Out
u <- c(5,2,8)
v <- c(1,3,9)
u>v
## [1] TRUE FALSE FALSE
Below create two vectors y and z and compare them.
#E- A vector is substantially a list of variables. A vector consists of a collection of numbers, arithmetic expressions, logical values or character strings.
#E- y & z became vectors when the list of variables were attached to them.
#E- y is a vector created with only even #'s, and z is a vector of only Odd #'s
#E- each list is compared to the other list in order. Hence why the first 3 function are true and the last 1 is false.
a <- c(2,4,6,8)
b <- c(1,3,5,9)
a>b
## [1] TRUE TRUE TRUE FALSE
Above, the > function was applied to u[1] and v[1], resulting in TRUE, then to u[2] and v[2], resulting in FALSE, and so on.
A key point is that if an R function uses vectorized operations, it, too, is vectorized, thus enabling a potential speedup. Here is an example:
#E- (5+1, 2+1, 8+1)
#E- What the function is saying is that [For every function of x =, which in this case is 'u', return x +1. ]
w <- function(x) return(x+1)
w(u)
## [1] 6 3 9
w(v)
## [1] 2 4 10
Here, w() uses +, which is vectorized, so w() is vectorized as well. As you can see, there is an unlimited number of vectorized functions, as complex ones are built up from simpler ones.
m <- function(x) return(x**2+1)
#E- What the function is saying is that [For every function of x =, which in this case is 'u & v', return (x*2) + 1.]
#E- '**' multiples the value as an exponent instead of a regular multiplication.
#E- '**'- exponent multiplication
#E- '*'- regular multiplication
m(u)
## [1] 26 5 65
m(v)
## [1] 2 10 82
Now create a functions that returns x^2 -1
#Enter your answer here
n <- function(x) return(x**2-1)
n(u)
## [1] 24 3 63
n(v)
## [1] 0 8 80
Let’s apply the function for rounding to the nearest integer to an example vector y:
#E- these values ae rounded to the nearest integer but with no specific direction, the direction (up,down) is random.
#E- round(x, digits = 0): Rounds values to specified number of decimal places.
#E- ceiling(x): Rounds values up to nearest integer.
#E- floor(x): Rounds values down to nearest integer.
y <- c(1.2,3.9,0.4)
z <- round(y)
z
## [1] 1 4 0
floor(y)
## [1] 1 3 0
Let’s apply the function for rounding to the nearest integer to an example vector r<-c(0.25,0.9,2.1,4.7,5.1):
#Enter your answer here
r<-c(0.25,0.9,2.1,4.7,5.1)
round(r)
## [1] 0 1 2 5 5
Since we know that R has no scalars, let’s consider vectorized functions that appear to have scalar arguments.
#E- scaler is the second part 'c'
#E- What the formula is saying is that "x + c' the scalar in (x,c)' squared.
#E- [(1+1)^2,(2+1)^2,(3+1)^2]
f<-function(x,c) return((x+c)^2)
f(1:3,0)
## [1] 1 4 9
f(1:3,1)
## [1] 4 9 16
Create a function that returns (x-c)^2 and calculate f(1:3,0) and f(1:3,1)
#Enter your answer here
f<-function(x,c) return((x-c)^2)
f(1:3,0)
## [1] 1 4 9
f(1:3,1)
## [1] 0 1 4
2.6.2 Vector In, Matrix Out
The vectorized functions we’ve been working with so far have scalar return values. Calling sqrt() on a number gives us a number. If we apply this function to an eight-element vector, we get eight numbers, thus another eight element vector, as output. But what if our function itself is vector-valued, as z12() is here:
z12 <- function(z) return(c(z,z^2))
Applying z12() to 5, say, gives us the two-element vector (5,25). If we apply this function to an eight-element vector, it produces 16 numbers:
#E- the function return the z value, then the z value calculation after all the values are displayed first.
x <- 1:8
z12(x)
## [1] 1 2 3 4 5 6 7 8 1 4 9 16 25 36 49 64
Compute z12(x2) where x2<-1:5. Explain the output.
#Enter your answer here
#E- for all the values of the vector x2 (1,2,3,4,5), return each variable Squared.
x2<- 1:5
z12(x2)
## [1] 1 2 3 4 5 1 4 9 16 25
It might be more natural to have these arranged as an 8-by-2 matrix, which we can do with the matrix function:
#E- Matrix() created a small table with the 8 values as rows, and the returns into 2 columns
matrix(z12(x),ncol=2)
## [,1] [,2]
## [1,] 1 1
## [2,] 2 4
## [3,] 3 9
## [4,] 4 16
## [5,] 5 25
## [6,] 6 36
## [7,] 7 49
## [8,] 8 64
Create a similar matrix using z12(x2)
#Enter your answer here
matrix(z12(x2),ncol=2)
## [,1] [,2]
## [1,] 1 1
## [2,] 2 4
## [3,] 3 9
## [4,] 4 16
## [5,] 5 25
2.7 NA and NULL Values
2.7.1 Using NA
In many of R’s statistical functions, we can instruct the function to skip over any missing values, or NAs. Here is an example:
x <- c(88,NA,12,168,13)
x
## [1] 88 NA 12 168 13
#E- since there is a NA value in the vector, the mean is not able to be calculated, hence why it needs to be taken care of.
mean(x)
## [1] NA
#E- na.rm= give you the decision to eliminate all the NA values, if you put T/ TRUE they are eliminated, if your put F/FALSE they stay.
#E- since all the NA value is eliminated then the mean is able to be calculated.
mean(x,na.rm=T)
## [1] 70.25
#E- NULL- is the absence of data so when the mean() is run, the NULL is skipped over.
x <- c(88,NULL,12,168,13)
mean(x)
## [1] 70.25
Enter your comments explaining the output obtained in the previous two exercises.
Let us now work with the mode:
#E- even doe there is an NA valu, since the majority of the vector is numeric, then the output for mode will be as such.
x <- c(5,NA,12)
mode(x[1])
## [1] "numeric"
mode(x[2])
## [1] "numeric"
y <- c("abc","def",NA)
#E- regardless of there being an NA value, the majority of the vectors mode is "Character" so the input will be the same.
mode(y[2])
## [1] "character"
mode(y[3])
## [1] "character"