Select multiple elements from a list - r

I have a list in R some 10,000 elements long. Say I want to select only elements, 5, 7, and 9. I'm not sure how I would do that without a for loop.
I want to do something like mylist[[c(5,7,9]] but that doesn't work. I've also tried the lapply function but haven't been able to get that working either.

mylist[c(5,7,9)] should do it.
You want the sublists returned as sublists of the result list; you don't use [[]] (or rather, the function is [[) for that -- as Dason mentions in comments, [[ grabs the element.

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

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 R lapply on a list to remove elements from the list

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]

How to select all elements of a list at a particular nested Level?

Or put it differently: How can I use the [[ operator in a nested list?
You can consider this as a follow-up question on this one, when I asked how to determine the depth level of a list. I got some decent answers from #Spacedman and #flodel who both suggested recursive functions. Both solutions where quite similar and worked for me.
However I haven't figured out yet what to do with the information I get from these functions. Let's say I have a list nested at level i and I want to get back a list that contains all i-th level elements, like this:
myList$firstLevel$secondLevel$thirdLevel$fourthLevel
# fourthLevel contains 5 data.frames and thirdLevel has
# three elements
How can I get back all 15 data.frames from mylist?
I am trying to use e.g.
lapply(mylist,"[[",2)
but obviously I just get the second element of all list elements at the first level.
EDIT: I found the following in the help of extract respectivel ?"[[" but can't really wrap my head around it so far:
"[[ can be applied recursively to lists, so that if the single index i is a vector of length p, alist[[i]] is equivalent to alist[[i1]]...[[ip]] providing all but the final indexing results in a list."
EDIT:
Don't want to end up nesting loops like this.
o <- list()
i=1
for (i in 1:2){
o[[i]] <- mylist[[c(i,1,1,1)]]
}
I found the answer in the meantime. Can't say say I did it on my own. This
link gives an elaborate explanation how to use another (complex) recursive function to linearize the whole nested list wad.
What's really nice about the solution provided by Akhil S. Behl: it deals with the fact that data.frames are lists too and recursing can stop before data.frames. It turned out that this was one of my major problems before.

Resources