0

I'm trying to build quite a complex loop in R.

I have a set of data set as an object called p_int (p_int is peak intensity). For this example the structure of p_int i.e. str(p_int) is:

num [1:1599]

The size of p_int can vary i.e. [1:688], [1:1200] etc.

What I'm trying to do with p_int is to construct a complex loop to extract the monoisotopic peaks, these are peaks with certain characteristics which will be extracted into a second object: mono_iso:

  1. search for the first eight sets of data results in p_int. Of these eight, find the set of data with the greatest score (this score also needs to be above 50).
  2. Once this result has been found, record it into mono_iso.
  3. The loop will then fix on to this position of where this result is located within the large dataset. From this position it will then skip the next result along the dataset before doing the same for the next set of 8 results.

So something similar to this:

16 Results: 100 120 90 66 220 90 70 30 70 100 54 85 310 200 33 41

** So, to begin with, the loop would take the first 8 results:

100 120 90 66 220 90 70 30

**It would then decide which peak is the greatest:

220

**It would determine whether 220 was greater than 50

IF YES:  It would record 220 into "mono_iso"
IF NO: It would move on to the next set of 8 results

**220 is greater than 50... so records into mono_iso

The loop would then place it's position at 220 it would then skip the "90" and begin the same thing again for the next set of 8 results beginning at the next data result in line: in this case at the 70:

70 30 70 100 54 85 310 200

It would then record the "310" value (highest value) and do the same thing again etc etc until the end of the set of data.

Hope this makes perfect sense. If anyone could possibly help me out into making such a loop work with R-script, I'd very much appreciate it.

4

1 回答 1

3

Use this:

mono_iso <- aggregate(p_int, by=list(group=((seq_along(p_int)-1)%/%8)+1), function(x)ifelse(max(x)>50,max(x),NA))$x

This will put NA for groups such that max(...)<=50. If you want to filter those out, use this:

mono_iso <- mono_iso[!is.na(mono_iso)]
于 2013-08-05T16:03:28.463 回答