-1

Im pretty new in this so would welcome any suggestions.

I have three columns in a data frame, one showing chromosome location and the next two showing the beginning and end of the chromosome location (i.e. chr1 1100 1200).

The first ten rows show data for chr1 but after that there is data for chr2 and so on.

I have created a loop and i want to apply it for the chr1 rows only. And then I want to repeat the same things for chr2.

So far i have written the following which doesn't seem to work: for(i in data.frame$chr1).

Any other suggestions?

Thanks

  • 2
    I am pretty sure someone can easily help you, but please see https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example for some tips on formatting your question. Some sample data with dput() would be very helpful. Also, for loops can often be avoided in R. What is it you really want to do? – Florian Jul 16 '17 at 15:26
  • If you're set on using a loop I think you would want to do `for (n in unique(data.frame$column.name))` – roarkz Jul 16 '17 at 16:51

1 Answers1

1

You can use the split function. It will split along the chromosome variable and you can use sapply or lapply functions to iterate through each list element (which now holds data for individual chromosome). If you provide a reproducible example it's trivial to show how to implement this.

Roman Luštrik
  • 69,533
  • 24
  • 154
  • 197