I have a function where its argument is a list. This argument is executed for different values. My original feature is very complicated. Therefore, I posted an example similar to part of my function.
An example here is multiplication (just to show an idea). I think it forcan't work here, because the loop fortakes element by element, while I need to take two different list items at the same time
The whole idea:
how to do this piece of code xxx <- myfun(z[[1]][k,j],z[[2]][k,i]))for an arbilary number of elements, instead of doing it manually. For example, if I have a 3matrix, then I would write code as follows:
xxx <- myfun(z[[1]][k,j],z[[2]][k,i], z[[3]][k,j]). How to do this automatically is not manual.
For example, zthis is a list of matrices. I would like to run my function for the specific values of these matrices. That is, every time I would like my function to select certain elements of my matrices. Here is my code:
x <- c(0,1,2,
0,0,1,
0,0,0)
x <- matrix(x,3,3)
y <- c(0,2,3,
0,0,1,
0,0,0)
y <- matrix(y,3,3)
z <- list(x,y)
myfun <- function(f=list()){
s <- length(f)
xy <- list()
for(i in 1:s){
xy[[i]] <- 2*f[[i]]
}
xy
}
Here is my attempt.
note that
Here, I would like to take certain function values of the matrices xand yand insert them in the list of my function. I would like it to work for an arbitrary number of list items. That is, sometimes I have matrices 3or 4. Therefore, the number of matrices is not fixed.
## I try the following and it works great, however, how to make it work for any number of elements.
xxx <- list()
for(i in 1:2){
for(k in 1:3){
for(j in 1:3){
xxx[[i]] <- myfun(f=list(z[[1]][1,2],z[[2]][1,2]))
}
}
}
Any help please?