How do I apply a function with multiple arguments over a list? - r

For example, I want to apply the intersect() function to every element (dataset) of a list. I want each element compared to this aother dataset, data1.
I know I can use a for loop, but I was thinking that I could use lapply. However, I need to hold one of the arguments constant. How can I do so?
This doesn't work:
> lapply(list(winnepennninckx, brunner), intersect(,selectG))

Related

applying function to a series of items in a list of lists

I have a function that needs to be applied to the same item in a list which is in a list pool. it is as follows:
myFUN<-function(x) {
myRESULT<-sqrt(sd(x)/50)
return(myRESULT)
}
i need to apply myFUN to a list such as:
big.list[[i]][["smaller.list"]][["smallest.list"]]
here, the thing is that there are 1500 different i in big.list and each of them have smaller.list and smallest.list in them. in other words, smallest.list is a list in smaller.list while smaller.list is a list in i and i is a list in big.list. also is are numbers from 1 to 1500.
I need to apply myFUN to each of the is and get the mean of them.
An option is to loop through the list, extract the smaller.list and smallest.list, apply the function. and then get the mean
mean(sapply(big.list, function(x) myFUN(x[["smaller.list"]][["smallest.list"]])))

multiplying two lists of matrices in continuos form

I have two lists of matrices and I want to multiply the first element of the first list with the first element of the second list and so on, without writing every operatios due to may be a large number of elements on each list (both lists have the same length)
this is what I mean
'(colSums(R1*t(M1))),(colSums(R2*t(M2))),...(colSums(Rn*t(Mn)))'
Do I need to create an extra list?
Although first I must be able to transpose the matrices of one of the lists before multiplying them. The results will be used for easier operations.
I already tried to use indexes and loops and doesn't work,
first tried to transpose matrices in one list like this (M is one of the lists and the other is named R, M contains M1,M2,..Mn and the same for list R)
The complete operation looks like this:
'for (i in 1:length(M)){Mt<-list(t(M[[i]]))}'
and only applies it to the last element.
The full operation looks like this:
'(cbind((colSums(R1*t(M1))),(colSums(R2*t(M2))),...(colSums(Rn*t(Mn))))'
any step of these will be useful
you could use the rlist package.
The function
list.apply(.data, .fun, ...)
will apply a function to each list element.
You can find documentation at [https://cran.r-project.org/web/packages/rlist/rlist.pdf][1].

How to remember which variables are in a list

I have a huge list in which I put different variables in order to apply the same function to all of them.
In a next step I want to apply specific functions to specific elements of the list, i.e. all functions used vary from element to element within the list.
How can I do this? My first idea was (see my other question, Reassign variables to elements of list) to split the list into the original variables again. This can be done.
But I was recommended to keep the items in the list instead. My questions is: How can I access each variable quickly by doing that? One idea would be to use the names attribute of the list in the beginning and fill it with a vector of the original variable names. However, by doing that it would be much longer later on to type list["name_x"] than just typing name_x assuming name_x is globally available.
What is the most efficient way to deal with my problem?

Passing undetermined number of arguments in R to the order() function

I've gathered that the order function in R can be used to sort rows of a data frame/matrix by one or more columns of that object. The columns are passed as separate arguments to order, and order can handle a variable number of arguments.
I would like to sort a data frame by all its columns, but I don't know the names or the number of columns in the data frame beforehand. In Python, one can unpack a list of objects as the arguments to a function (e.g. zip(*mylist) is zip(mylist[0], mylist[1], etc...)). Is there a similar way to do so in R? It would be nice to "unpack" the columns of a matrix when I call order.
Is there another way in R to sort by multiple columns besides passing an arbitrary number of parameters?
more thoughts:
It seems like I cannot just package multiple unnamed items into a single object to pass to order. Nor can I think of a way to use a for loop, apply, or do.call to make arbitrary numbers of objects. There's something here: http://r.789695.n4.nabble.com/custom-sort-td888802.html.
Or... should I write a for loop to call order on each column, starting with the least priority one and ending with the column that would've been the first argument to order, reordering the rows each time and making sure that order sorts stably?
Thanks.
in python calling fun(*args,**kwargs) specifies the list of positional arguments (*args) and arguments to be matched by name (kwargs).
A similar call in R is do.call(fun,arglist). Unlike python, you cant mix regular and special arguments (e.g. fun(a=1,*args)) and the second argument to do.call is can have elements that are matched by name or position (e.g. do.call(fun,list(2,x=3)))
To complete the example, since data.frames inherit from lists, you can simply call 'order(df)' to order on all the columns sequentially (as long as none of the names of the fields in your data.frame match the formal arguments of order 'na.last' and 'decreasing')

How does one pass multiple data types in llply?

I have a function that requires both a S4 object and a data frame as arguments.
But functions like lapply and llply will only allow one list and one function.
example: new_list=llply(list, function)
I could make a single list with alternating S4 object and data but llply will push one list item at a time which means that it will either be the S4 object or the data (the function cannot perform with just one or the other).
In some sense what I am looking for is akin to a 2D list (where each row has the S4 obj and data and a row gets pushed through at a time).
So how would I make this work?
Here's a more general version of my problem. If I have a function like so:
foobar <- function(dat, threshold=0.5, max=.99)
{
...
}
and I wanted to push a list through this function, I could do:
new_list=llply(list, foobar)
but if I also wanted to pass a non-default value for threshold or max, how would I do so in this context?
Functions like lapply typically have a ... parameter of arguments which get passed to the function. Eg:
lapply(list, foobar, somearg='nondefaultvalue')
If you have multiple varying parameters (eg a different somearg value for each element in list), then you would either pack them as pairs in a list, or turn to a function like mapply:
mapply(foobar, list, somearg=c('vectorof', 'nondefault', 'values')
May be you can try this:
Make each list item itself a list, which contains a S4 object and a data frame.
Just a suggestion, I'm not quite sure if this works.

Resources