Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extract elements from a list with mixed elements

Tags:

list

r

vector

I have a list in R with the following elements:

[[812]] [1] ""             "668"          "12345_s_at" "667"          "4.899777748"  [6] "49.53333333"  "10.10930207"  "1.598228663"  "5.087437057"   [[813]] [1] ""            "376"         "6789_at"  "375"         "4.899655078" [6] "136.3333333" "27.82508792" "2.20223398"  "5.087437057"  [[814]] [1] ""             "19265"        "12351_s_at" "19264"        "4.897730912"  [6] "889.3666667"  "181.5874908"  "1.846451572"  "5.087437057"  

I know I can access them with something like list_elem[[814]][3] in case that I want to extract the third element of the position 814. I need to extract the third element of all the list, for example 12345_s_at, and I want to put them in a vector or list so I can compare their elements to another list later on. Below is my code:

elem<-(c(listdata)) lp<-length(elem) for (i in 1:lp) {     newlist<-c(listdata[[i]][3]) ###maybe to put in a vector     print(newlist)  } 

When I print the results I get the third element, but like this:

  [1] "1417365_a_at"   [1] "1416336_s_at"   [1] "1416044_at"   [1] "1451201_s_at" 

so I cannot traverse them with an index like newlist[3], because it returns NA. Where is my mistake?

like image 658
Layla Avatar asked Sep 30 '12 14:09

Layla


People also ask

What is the list in R?

Lists are the R objects which contain elements of different types like − numbers, strings, vectors and another list inside it. A list can also contain a matrix or a function as its elements. List is created using list() function.


2 Answers

If you want to extract the third element of each list element you can do:

List <- list(c(1:3), c(4:6), c(7:9)) lapply(List, '[[', 3)  # This returns a list with only the third element unlist(lapply(List, '[[', 3)) # This returns a vector with the third element 

Using your example and taking into account @GSee comment you can do:

yourList <- list(c("","668","12345_s_at","667", "4.899777748","49.53333333",        "10.10930207", "1.598228663","5.087437057"),      c("","376", "6789_at",  "375",  "4.899655078","136.3333333",        "27.82508792", "2.20223398",  "5.087437057"),      c("", "19265", "12351_s_at", "19264", "4.897730912",        "889.3666667", "181.5874908","1.846451572","5.087437057" ))  sapply(yourList, '[[', 3) [1] "12345_s_at" "6789_at"    "12351_s_at" 

Next time you can provide some data using dput on a portion of your dataset so we can reproduce your problem easily.

like image 82
Jilber Urbina Avatar answered Oct 04 '22 15:10

Jilber Urbina


With purrr you can extract elements and ensure data type consistency:

library(purrr)  listdata <- list(c("","668","12345_s_at","667", "4.899777748","49.53333333",        "10.10930207", "1.598228663","5.087437057"),      c("","376", "6789_at",  "375",  "4.899655078","136.3333333",        "27.82508792", "2.20223398",  "5.087437057"),      c("", "19265", "12351_s_at", "19264", "4.897730912",        "889.3666667", "181.5874908","1.846451572","5.087437057" ))  map_chr(listdata, 3) ## [1] "12345_s_at" "6789_at"    "12351_s_at" 

There are other map_ functions that enforce the type consistency as well and a map_df() which can finally help end the do.call(rbind, …) madness.

like image 36
hrbrmstr Avatar answered Oct 04 '22 15:10

hrbrmstr