Compared vectors u and v. It is asking if vector u is bigger than v. The output is that for first value it is true because 5>1 and for the second and third value it is false.
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.
3 vectors have been created, when y and z are compared, code is asking if y is equal greater or less than z, and program is answering me.Vector q has one row with 3 values while y nd z have 4 values, for this reason it is not possible to compare vector q wiuth the other two vectors.
y <- c(4,15,67,3)
z <- c(15,27,2,89)
q <- c(3,7,12)
y == z
## [1] FALSE FALSE FALSE FALSE
y<z
## [1] TRUE TRUE FALSE TRUE
y>z
## [1] FALSE FALSE TRUE FALSE
#z == q
#z>q
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:
Vector w has been created adding one to values of the vector you select, in this case vector u so Since vector u values are (5,2,8), vector w values will be (6,3,9)
w <- function(x) return(x+1)
w(u)
## [1] 6 3 9
At this point selecting vector v instead of vector u. vector v values (1,3,9) vector w values will be (2,4,10)
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.
Vector m has been created, in function of another vector, values of the vector selected will be elevated to the potential of 2 and summed to 1.
m <- function(x) return(x**2+1)
Selecting vector u as the default one, the output will be (52+1,22+1,8^2+1)
m(u)
## [1] 26 5 65
Selecting vector v as the default one, the output will be (12+1,32+1,9^2+1)
m(v)
## [1] 2 10 82
Now create a functions that returns x^2 -1 Vector p has been created, in function of another vector, values of the vector selected will be elevated to the potential of 2 and rested to 1
p <- function(x) return(x**2-1)
Selecting vector u as the default one, the output will be (52-1,22-1,8^2-1)
p(u)
## [1] 24 3 63
Selecting vector v as the default one, the output will be (12-1,32-1,9^2-1)
p(v)
## [1] 0 8 80
Let’s apply the function for rounding to the nearest integer to an example vector y: Round function, rounds values to its closest hole number.
y <- c(1.2,3.9,0.4)
z <- round(y)
z
## [1] 1 4 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):
r<-c(0.25,0.9,2.1,4.7,5.1)
k <- round(y)
k
## [1] 1 4 0
Since we know that R has no scalars, let’s consider vectorized functions that appear to have scalar arguments. vector f has been created in function of variables x and c. In this case being x 1:3 and c zero.Being the equation (x+c)^2
f<-function(x,c) return((x+c)^2)
f(1:3,0)
## [1] 1 4 9
Changing the value of c from zero to 1.
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)
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 eightelement vector, as output. But what if our function itself is vector-valued, as z12() is here:
Vector z12 has been created in function of variable z. It will output the value of z and the square root of z. If z is 4 the output will be a two element vector(4,16). In this example z is the vector v.
z12 <- function(z) return(c(z,z^2))
z12(v)
## [1] 1 3 9 1 9 81
Being z the vector x which is the values from 1 to 8.
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.
Being z the vector x2 which is the values from 1 to 5.
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:
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) So instead of selecting vector x, vector x2 is selected.
matrix(z12(x2),ncol=2)
## [,1] [,2]
## [1,] 1 1
## [2,] 2 4
## [3,] 3 9
## [4,] 4 16
## [5,] 5 25
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
since there is a missing value in the vector, the program can not compute the mean of it.
mean(x)
## [1] NA
So, in order to compute the mean, missing values could be excluded using na.rm=T
mean(x,na.rm=T)
## [1] 70.25
values could be excluded using NULL too.
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:
Function mode return a character string giving the (storage) mode of the object
x <- c(5,NA,12)
mode(x[1])
## [1] "numeric"
Since the storage mode of the object is numeric, even if program is asked for the mode of the NA will say it is numeric.
mode(x[2])
## [1] "numeric"
vector y is created
y <- c("abc","def",NA)
y
## [1] "abc" "def" NA
Function mode return a character string giving the (storage) mode of the object. So since vector is composed of categorical values the output will be that the storage mode is “Character”
mode(y[2])
## [1] "character"
Since the storage mode of the object is “character”, even if program is asked for the mode of the NA will say it is “character”.
mode(y[3])
## [1] "character"