Using R lapply on a list to remove elements from the list - r

I have a list I want to process to remove elements not meeting certain criteria. I'd use a loop but that is too slow so I assume lapply might be better.
assuming the last thing function y does is give a value of 1 or 2 to variable x how would I modify
lapply(list,functiony, z=valueA, a=valueB) to give back a list of only the elements of the list that are of value x=1?

Try mylist[sapply(mylist, functiony, z=valueA, a=valueB)==1]

Related

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 do I apply a function with multiple arguments over a list?

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))

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?

R lapply into data frame

I have a list contains list as elements. I want to convert all elements into data frame. Instead of using for loop. I used lapply function as follow:
myDF=lapply(mylist,FUN=as.data.frame)
However it's not converting.
class(myDF[1])
still returns list.
Any ideas? Thank you so much for your help,
To look at the first element of the list as-is (i.e. not as a list) you will want to use [[ instead of [. So
class(myDF[[1]])
will tell you the class of the first list element in myDF.
Another way to see this is to look at the difference between myDF[1] and myDF[[1]]. myDF[1] returns the first element of the list as a single-element list, whereas myDF[[1]] returns the first element of the list as itself. See help(Extract) for more.

Using a list of matrix names

I have 75 matrices that I want to search through. The matrices are named a1r1, a1r2, a1r3, a1r4, a1r5, a2r1,...a15r5, and I have a list with all 75 of those names in it; each matrix has the same number of rows and columns. Inside some nested for loops, I also have a line of code that, for the first matrix looks like this:
total <- (a1r1[row,i]) + (a1r1[row,j]) + (a1r1[row,k])
(i, j, k, and row are all variables that I am looping over.) I would like to automate this line so that the for loops would fully execute using the first matrix in the list, then fully execute using the second matrix and so on. How can I do this?
(I'm an experienced programmer, but new to R, so I'm willing to be told I shouldn't use a list of the matrix names, etc. I realize too that there's probably a better way in R than for loops, but I was hoping for sort of quick and dirty at my current level of R expertise.)
Thanks in advance for the help.
Here The R way to do this :
lapply(ls(pattern='a[0-9]r[0-9]'),
function(nn) {
x <- get(nn)
sum(x[row,c(i,j,k)])
})
ls will give a list of variable having a certain pattern name
You loop through the resulted list using lapply
get will transform the name to a varaible
use multi indexing with the vectorized sum function
It's not bad practice to build automatically lists of names designating your objects. You can build such lists with paste, rep, and sequences as 0:10, etc. Once you have a list of object names (let's call it mylist), the get function applied on it gives the objects themselves.

Resources