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.
Related
I am trying to obtain a subset of a named list, based on element present in another list.
nammedlist<-list( "a"=c(1,2,3,4), "b"=c(2,4,5), "c"=c(9,5,3,2))
selection<-c("a","c")
desired output:
namedlist2<-list( "a"=c(1,2,3,4), "c"=c(9,5,3,2))
I am considering writing a for loop checking for each name if it is present and then extracting it. But their must be a cleaner way to do this.
You can use the names as an index:
nammedlist[selection]
will give you what you want.
Note that you use single brackets, not double brackets. Single brackets mean that you want a subset of the list. Double brackets mean you want to extract an element from the list.
I'm currently working with a list of adjectives from rcorpora that looks like this when I call corpora("words/adjs"):
$description
[1] "A list of English adjectives."
$adjs
[1] "Aristotelian" "Arthurian" "Bohemian" "Brethren" "Mosaic"
[6] "Oceanic" "Proctor" "Terran" "Tudor" "abroad"
There are 1000 words in $adjs but for brevity I only put the first 10. I want to individually save all of the adjectives as their own element in a new list, because the way it is now $adjs is the second element in list words/adjs and a massive character, according to R when I do class(adjectives$adjs). My goal is to be able to randomly go through this new list of adjectives, pick one out, and then use that in my code, but I'm having trouble creating this list of separate, discrete elements. Any help would be appreciated.
Try this:
my_adj <- corpora("words/adjs")$adjs
The corpora package looks like it gives you a nested list (first list is description second list is the adjectives). Nested lists are tough to work with so I like to pull objects out by name or index.
If you need to know if you're working with a nested list use the str(...) function, like this:
str(corpora("words/adjs"))
This will give you a good summary of your R object and then you can dig in and pull out what you need. str(...) is a great function for all objects, not just lists!
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].
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]
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.