Opposite of array_tree in purrr package? - r

I was wondering if there is a function that is the opposite of the array_tree function from the purrr package.
Specifically I would like a function (lets call it list_untree -better suggestions welcome) that will do the following. If a is a multidimensional array and m is some vector specifying the margins for array_tree.
l <- array_tree(a, margin=m)
a <- list_untree(l, margin=m)
How can I write something like this efficiently if it does not already exist.
For More Motivation:
I am particularly interested in doing computations on l using the map function and then plugging the result into the list_untree function.

Related

Functions that takes and returns vector in R

I want to create a function to help me convert a vector containing different values of Ghanaian Cedi to Hungarian Forint (1 cedi = 57.06 forint). My function name is Currency; such that if I give the function a vector [1,2,3,4],where1,2,3,4 represents cedi, the function will return me Currency(1), Currency(2), Currency(3) ,Currency(4), which are forints.
I was thinking of using loop to create my function. Before that, I would like to know if there's any easier way to separate the vector?
Vectors are a native data type in R. A numeric value is actually a numeric vector with one element as far as R is concerned.
In other words, R works directly with vectors making loops or apply functions rarely useful for trivial operations. Multiplying by a constant is a standard vector operation.
The function below handles a vector just fine:
convert_currency <- function(cedi) {
cedi * 57.06
}
convert_currency(1:4)
#> [1] 57.06 114.12 171.18 228.24

Tilde Operator in map function

I have a question regarding the map function in R and the tilde operator ´~´
Why does this code only work that way:
iris_unique <- map(iris, ~length(unique(.)))
and not for example like this
iris_unique <- map(iris, length(unique(iris$Sepal.Length)))
Thanks in advance
Assuming that you are talking about map from the package purrr, this function is designed to map a function over a vector.
length(unique(iris$Sepal.Length)) is a specific value (35 for the standard iris dataset), so
iris_unique <- map(iris, length(unique(iris$Sepal.Length)))
is equivalent to
iris_unique <- map(iris, 35)
since 35 is not a function, this is probably not what you mean. However map() tries to make sense of it. The documentation says that if for the function parameter you pass it a "character vector, numeric vector, or list, it is converted to an extractor function", which means that 35 is converted to the function function(x){x[35]}, hence the net result is to extract the 35th observation of iris.
On the other hand, the documentation also describes how it translates formulas into functions. According to that, the formula ~length(unique(.)) is translated to the function function(x){length(unique(x))}. Since this is a function, it makes perfect sense to map it over a list or vector.

Handling matrices using Brobdingnag package

I need to build a matrix with extremely small entries.
So far I realized that the fastest way to define the kind of matrix that I need is:
Define a vectorized function of coordinates:
func = function(m,n){...}
Combine every possible coordinate using outer:
matrix = outer(1:100,1:100,FUN=func)
Having to deal with extremely small numbers I work in func's environment using brob numbers, its output will therefore be of the same type of a brob:
typeof(func(0:100,0:100) )
[1] "S4"
If I directly plug two vectors 0:100 in my function func it returns a vector of brobs but if I try to use it with outer I get the error:
Error in outer(1:100, 1:100, FUN = func) : invalid first argument
I suppose this is because package Brobdingnag can somehow deal with vectors but not with matrices. Is it right? Is there any way to make it work?

IFFT function in R?

In Matlab, there is an ifft function (Inverse fast Fourier transform) - details.
In particular, the following:
ifft(X,n,dim)
Which returns the inverse DFT of X across the dimension dim.
In R, there is a similar function apart of the signal package - details
However it only allows for the x input array, as follows:
ifft(x)
Question:
Is there any way to include the extra dimension, such as dim in the Matlab function, with R?
Thank you so much for taking a look at my question, very helpful.
Are you looking for ? mvfft (with inverse = TRUE)?
mvfft does the (inverse) FFT by columns, but you can reshape your data:
t for matrix transpose
aperm for an array extension of t (dimension permutation)
you can reshape your array to a matrix by dim<-
(package arrayhelpers has convenience functions for such conversion of an array into a matrix and back).

Multidimensional binding

For dealing with two-dimensional matrices, rbind and cbind are useful functions. Are there more generic functions to perform the same operation in more dimensions? Suppose I have data like this:
data <- lapply(c(11,22,33), function(i) matrix(i, nrow=2, ncol=4))
What I'd like to obtain is this:
data <- do.call(c, data)
dim(data) <- c(2, 4, 3)
but without having to work out all the dimensions myself.
Is there a function providing this functionality, either built-in or as part of a reasonably common package? Or do you want to share your own ideas of how such a function could be implemented most elegantly?
Bonus points:
If the function gives some control over the order of dimensions, then a subsequent call to aperm could be avoided.
It would be nice if it could operate by either passing multiple function arguments or a list of arguments, although using do.call or list, either one will suffice.
I'd like to use such a function as the .combine argument to a foreach call. So it should be able to construct multi-dimensional matrices using calls of the form f(f(f(a, b), c), d) (each call takes exactly two arguments, the first usually the result of the previous call) or even f(f(a, b), c, d) (more than two arguments, the first still might be the result of the previous call), with a, b, c, d all of the same size, resulting in a matrix with a dimension 1 higher than the dimensions of these and a size of 4 in that dimension, corresponding to the 4 elements a through d.
The abind package has precisely this function, with most of the features you mention, although I haven't checked all of them in detail.
At the very least, it would give you a start on how one would implement something along these lines.

Resources