1

Ok So I have two sets of Mass Spec Data and I am trying to format and plot it but I am not sure how to finish the formating.

The list currently looks like this:

[[1]]
[1] "3 999"   "4 811"   "5 986"   "6 745"   "10 287"  "18 163"  "25 114"  "26 164" 
[9] "27 6"    "29 10"   "34 264"  "36 186"  "38 263"  "39 21"   "41 11"   "43 6"   
[17] "44 25"   "45 10"   "51 12"   "55 3"    "57 4"    "63 11"   "64 4"    "65 77"  
[25] "66 5"    "69 4"    "77 13"   "78 5"    "79 5"    "85 5"    "89 22"   "90  17"  
[33] "91 850"  "92 75"   "93 5"    "100 143" "103 2"   "104 6"   "105 6"  "106  12" 
[41] "116 10"  "214 185"

[[2]]
[1] "2 15"  "4 999"

And I am looking to plot this:

Plot 1:

3,999  4,811   5,986   6,745   10,287  18,163  25,114 etc...

Plot 2:

2,15 4,999 

Any Thoughts?

data structure:

L <- list(
c("3 999", "4 811", "5 986", "6 745", "10 287", "18 163", "25 114", 
"26 164", "27 6", "29 10", "34 264", "36 186", "38 263", "39 21", 
"41 11", "43 6", "44 25", "45 10", "51 12", "55 3", "57 4", "63 11", 
"64 4", "65 77", "66 5", "69 4", "77 13", "78 5", "79 5", "85 5", 
"89 22", "90  17", "91 850", "92 75", "93 5", "100 143", "103 2", 
"104 6", "105 6", "106  12", "116 10", "214 185"),
c("2 15", "4 999")
)
Community
  • 1
  • 1
Zach Eisner
  • 114
  • 9
  • 1
    Possible duplicate of [Plot two graphs in same plot in R](http://stackoverflow.com/questions/2564258/plot-two-graphs-in-same-plot-in-r) – s952163 Jul 19 '16 at 00:59
  • My Question is more how to format the list of "x space y" , "x space y" into a vector of plottable points – Zach Eisner Jul 19 '16 at 01:00
  • The list is actually 240,000 elements long...i just gave 2 as an example – Zach Eisner Jul 19 '16 at 01:01
  • 1
    @ZachEisner - i've added reproducible data to your question - if you could do the same next time that would be appreciated. – thelatemail Jul 19 '16 at 01:03
  • To convert the character strings to numeric something like: lapply(L, function(x) {as.numeric(gsub(" ", "", x))}) should work. Needed to wrap inside lapply for your vectors within a list. – Dave2e Jul 19 '16 at 01:05
  • using gsub for element one outputs `[1] "c(\"3999\",\"4811\",\"5986\",\"6745\",\"10287\",\"18163\",\"25114\",\"26164\",\"276\",\"2910\",\"34264\",\"36186\",\"38263\",\"3921\",\"4111\",\"436\",\"4425\",\"4510\",\"5112\",\"553\",\"574\",\"6311\",\"644\",\"6577\",\"665\",\"694\",\"7713\",\"785\",\"795\",\"855\",\"8922\",\"9017\",\"91850\",\"9275\",\"935\",\"100143\",\"1032\",\"1046\",\"1056\",\"10612\",\"11610\",\"214185\")"` and when I apply as.numeric to that is gives NA. @Dave2e – Zach Eisner Jul 19 '16 at 01:09

2 Answers2

4

With a little bit of mucking around with scan() you can get matrix outputs, which should be simple to plot:

lapply(L, function(x)
  matrix(scan(text=paste(x,collapse=" "),what=numeric()),ncol=2,byrow=TRUE)
)

#Read 84 items
#Read 4 items
#[[1]]
#      [,1] [,2]
# [1,]    3  999
# [2,]    4  811
# ...
#
#[[2]]
#     [,1] [,2]
#[1,]    2   15
#[2,]    4  999
thelatemail
  • 81,120
  • 12
  • 111
  • 172
2

Given your list of arrays L from above:

strtodf<-function (list){
  slist<-strsplit(list," ")  #string split at the space
  x<-as.numeric(sapply(slist, FUN= function(x) {x[1]}) )
  y<-as.numeric(sapply(slist, FUN= function(x) {x[2]}) )
  df<-data.frame(x=x, y=y, stringsAsFactors = FALSE)
  return(df)
}

#returns a list of data frames
listofdf<-lapply(L, test3)
listofdf[[1]]  #returns the first data frame.

The function strtodf takes a vector of strings, slits each element at the space and then returns a two column data frame with the results. Since you have a list of vectors, the call needs to called within lapply.

Dave2e
  • 15,736
  • 17
  • 32
  • 37