aperm function not clear - r

So I'm trying to figure out what this function aperm() does. When I do aperm(a), where a is a matrix, I get its transpose; I get that. But what about this function:
aperm(a,c(3,1:2))
what does this do? when a is a 6*7 matrix this command doesn't work.
I dont understand the R example either.

You receive the error because you are telling aperm to permute a 3 dimensional array, but only providing it a 2 dimensional array (a matrix). You need a 3 dimensional array for your command to work:
Consider the following example:
myarray <- array( 1:24, dim=c(2,3,4),
dimnames=list(One=c('a','b'), Two=c('A','B','C'), Three=1:4) )
myarray
aperm(myarray, c(3,1,2))
This creates a 3 dimensional array with dimnames to help make it clearer, then permutes it. Notice the new order of the values.
Also this example:
> myarray[2,3,4]
[1] 24
>
> mynewarray <- aperm(myarray, c(3,1,2) )
> mynewarray[4,2,3]
[1] 24
>
To get the element with value 24 in the original array we take the 2nd row of the 3rd column of the 4th layer (or whatever you want to call the 3rd dimension).
But in the permuted array it is now the 4th row (that used to be layer) of the 2nd column (which used to be rows) of the 3rd layer (that used to be columns).

Related

What does index do in r?

I have a code I'm working with which has the following line,
data2 <- apply(data1[,-c(1:(index-1))],2,log)
I understand that this creates a new data frame, from the data1, taking column-wise values log-transformed and some columns are eliminated, but I don't understand how the columns are removed. what does 1:(index-1) do exactly?
The ":" operator creates an integer sequence. Because (1:(index-1) ) is numeric and being used in the second position for the extraction operator"[" applied to a dataframe, it is is referring to column numbers. The person writing the code didn't need the c-function. It could have been more economically written:
data1[,-(1:(index-1))]
# but the outer "("...")"'s are needed so it starts at 1 rather than -1
So it removes the first index-1 columns from the object passed to apply. (As MrFlick points out, index must have been defined before this gets passed to R. There's not default value or interpretation for index in R.
Suppose the index is 5, then index -1 returns 4 so the sequence will be from 1 to 4 i.e. and then we use - implies loop over the columns other than the first 4 columns as MARGIN = 2

how to repeat a function using different multiple input matrices

I want to use the function "nestedness(M)" from the "bipartite" R package. It calculates an index from a matrix (M). I have an array with 1000 matrices and I want to apply this function 1000 times varying the input matrix file each time. I have tried apply family functions but the solution does not come this way. I don't know how to vary the input of a function when it is not a number but a matrix. Any aid to put me on the way would be very very appreciated.
Lets say you have an array that is 3x3x3 ie 3 matrices that are each 3
rows and 3 columns. The dimensions of an array are c("row", "column",
"slice"). You can use apply over any of these dimensions. In your
case over the 3rd dimension will calculate your function over each
matrix. Here is the example array:
a <- array(1:27, dim = c(3,3,3))
Now calculate the max function for each slice (dimension 3) of the array
apply(a, 3, max)
[1] 9 18 27

R Compare each data value of a column to rest of the values in the column?

I would like to create a function that looks at a column of values. from those values look at each value individually, and asses which of the other data points value is closest to that data point.
I'm guessing it could be done by checking the length of the data frame, making a list of the respective length in steps of 1. Then use that list to reference which cell is being analysed against the rest of the column. though I don't know how to implement that.
eg.
data:
20
17
29
33
1) is closest to 2)
2) is closest to 1)
3) is closest to 4)
4) is closest to 3)
I found this example which tests for similarity but id like to know what letter is assigns to.
x=c(1:100)
your.number=5.43
which(abs(x-your.number)==min(abs(x-your.number)))
Also if you know how I could do this, could you expain the parts of the code and what they mean?
I wrote a quick function that does the same thing as the code you provided.
The code you provided takes the absolute value of the difference between your number and each value in the vector, and compares that the minimum value from that vector. This is the same as the which.min function that I use below. I go through my steps below. Hope this helps.
Make up some data
a = 1:100
yourNumber = 6
Where Num is your number, and x is a vector
getClosest=function(x, Num){
return(which.min(abs(x-Num)))
}
Then if you run this command, it should return the index for the value of the vector that corresponds to the closest value to your specified number.
getClosest(x=a, Num=yourNumber)

In R, how to produce an array containing sorted index of corresponding elements in another array?

In R, given an numeric array, how to produce another array which contains sorted index order into the original array? To clarify my question, consider an example: For an original array:
[33 11 55 22 44]
I want to produce another array:
[3 1 5 2 4]
Each element in the second array indicates sorted index of corresponding element in the first array.
In MATLAB, this can be done by using [B,XI]=sort(A); where XI is the wanted array.
Try the rank() function, found here.

How to get a value of a multi-dimensional array by an INCOMPLETE vector of indices

This question is very similar to
R - how to get a value of a multi-dimensional array by a vector of indices
I have:
dim_count <- 5
dims <- rep(3, dim_count)
pi <- array(1:3^5, dims)
I want to get an entire line, but with an automatic building of the address of this line.
For example, I would like to get:
pi[1,,2,2,3]
## [1] 199 202 205
You could insert a sequence covering the whole dimension in the appropriate slot:
do.call("[",list(pi,1,1:dim(pi)[2],2,2,3))
By the way, defining a variable called pi is a little dangerous (I know this was inherited from the previous question) -- suppose you tried a few lines later to compute the circumference of a circle as pi*diameter ...

Resources