Understanding R from PHP perspective: Arrays in R
R can be quite intimidating when you first start learning it from a PHP background. It has many other basic concepts that you don't see them in PHP, notably: vector.
What the hell is a vector? I just didn't get it when I first started. You see this word everywhere in R tutorials.
A vector, simply put, in PHP terms, is a value. It is the most basic R data object. There are six types of (atomic) vectors: logical, integer, double, complex, character and raw. Even when you write just one value in R, it becomes a vector of length 1 and belongs to one of these vector types.
1. Single Element Vector
# Atomic vector of type character: str("R") # Atomic vector of type logical: str(TRUE)
When we execute the above code, it produces the following result:
chr "R" logi TRUE
string(3) "PHP" bool(true)
You notice that 'characters' in R are 'strings' in PHP.
2. Multiple Elements Vector
In PHP, an array is multiple values in one single variable:
$cars = array("Volvo", "BMW", "Toyota");
We use the
array() function to create an indexed array. In R, it is a bit complicated. The most basic form of an array in R is vectors. R has
array() function but it is different from PHP. R
array() is a n-dimensional array (or three-dimensional array) in PHP. A one-dimensional array is called vectors in R, a two-dimensional array is called matrix in R. We use
c() function to create a one-dimensional array (we will call it vectors from now on).
Quoted from w3schools:
... the c function used for concatenating different values and vectors for creating longer vectors.
Vectors/ One-dimensional arrays
> x <- c(11, 12, 13) # Extract the 3rd value from the vector 'x': > x  13 # Replace the 3rd value in the vector 'x': > x <- 14 > x  14
> $x = [11, 12, 13] # Extract the 3rd value from the array 'x': echo $x; 13 # Replace the 3rd value in the array 'x': $x = 14; echo $x; 14
Factors (One-dimensional arrays)
Quoted from R-bloggers:
Factors are categorical variables that are super useful in summary statistics, plots, and regressions. They basically act like dummy variables that R codes for you.
> x <- c("male", "male", "male", "male", "male", "female", "female", "female", "female", "others", "others", "others", "others") > factor(x)  male male male male male female female female female others others others others Levels: female male others # Or we can see all the levels (categories) this way: > levels(factor(x))  "female" "male" "others" # factor is useful when we want to know the statistic of genders: > summary(factor(x)) female male others 4 5 4
$x = array("male", "male", "male", "male", "male", "female", "female", "female", "female", "others", "others", "others", "others"); print_r(array_count_values($x)); # Array ( [male] => 5 [female] => 4 [others] => 4 )
Matrices/ Two-dimensional arrays
Matrices (or two-dimensional arrays) are built from vectors (one-dimensional arrays).
Quoted from r-tutor:
A matrix is a collection of data elements arranged in a two-dimensional rectangular layout.
You can create a matrix in two ways:
- Use the command
matrix(vector, nrow = 3#3 , ncol = 2#2).
rbind()- in plain English: column-bind, row-bind
The R example below using row-bind:
> x <- c(11, 12, 13) > y <- c(55, 33, 12) > foo <- rbind(x, y) > foo [,1] [,2] [,3] x 11 12 13 y 55 33 12 # Extract the 1st row of foo: > foo[1,]  11 12 13 # Extract the 1st column of foo: > foo[,1] x y 11 55
$foo = [ [11, 12, 13], [55, 33, 12] ]; print_r($foo); # Array ( #  => Array (  => 11  => 12  => 13 ) #  => Array (  => 55  => 33  => 12 ) # ) # Extract the 1st row of foo: print_r($foo); # Array (  => 11  => 12  => 13 ) # Extract the 1st column of foo: $fistColumn = array_column($foo, 0); print_r($fistColumn); # Array (  => 11  => 55 )
Arrays/ n-dimensional arrays
Arrays, specifically in R, are similar to matrices but can have more than two dimensions. In other words, a set of stacked matrices of identical dimensions. Contrary to PHP, arrays are a generic term that covers one-dimensional and multi-dimensional arrays.
Quoted from w3school:
The vector variables that you have implemented at so far on the last tutorial are all single – dimensional / one dimensional objects. This is because they have only length but no other dimensions. Arrays can contain multi dimensional rectangular shaped data storage structure. “Rectangular” in the sense, each row is having the same length and similarly for each column and other dimensions. Matrices are a special type of two – dimensional arrays.
We can use
array() function to create arrays. It takes vectors as input and uses the values in the dim parameter to create an array. Its format:
c(numbers of row, numbers of column, numbers of set)
dim stands for
> x <- array(1:9, c(2,3,2)) > x , , 1 [,1] [,2] [,3] [1,] 1 3 5 [2,] 2 4 6 , , 2 [,1] [,2] [,3] [1,] 7 9 2 [2,] 8 1 3 # Extract the 1st metrix of x: > x[, , 1] [,1] [,2] [,3] [1,] 1 3 5 [2,] 2 4 6 # Extract the 1st row of 1st metrix of x: > x[, , 1][1,]  1 3 5
x[, , 1] or
x[, , 1][1,] is quite a pain due to the lack of readability. Personally I rarely use arrays in R to avoid confusions.
Quoted from Advanced R by Hadley Wickham:
Matrices are used commonly as part of the mathematical machinery of statistics. Arrays are much rarer, but worth being aware of.
# Assuming that you have created a three-dimensional array... # Extract the 1st row of 1st array of x: print_r($x); # Array (  => 1  => 3  => 5 )
Even for PHP alone, it can be intimidating reading three-dimensional arrays. Imagine this:
$x;... Generally I don't use deep indices to select an element. I use associated arrays instead for complex data.
Quoted from w3schools:
PHP understands multidimensional arrays that are two, three, four, five, or more levels deep. However, arrays more than three levels deep are hard to manage for most people.
I hope this article with R and PHP comparisons is helpful and giving you an idea if you ever stumble on the same learning curve. Let me know what you think and what you had struggled when first learning R. Any suggestions and errors, please leave a comment below.