Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Avoiding row-wise processing of data.frame in R

I was wondering what the best way is to avoid row-wise processing in R, most of row-wise stuff is done in internal C routines. For example: I have a data frame a:

  chromosome_name start_position end_position strand
1              15       35574797     35575181      1
2              15       35590448     35591641     -1
3              15       35688422     35688645      1
4              13       75402690     75404217      1
5              15       35692892     35693969      1

What I want is: based on whether strand is positive or negative, startOFgene as start_position or end_position. One way to avoid for loop will be to separate data.frame with +1 strand and -1 strand and perform selection. What can be other way for speed up? The method does not scale-up if one has certain other complicated processing per row.

like image 687
vinash85 Avatar asked Feb 19 '23 04:02

vinash85


1 Answers

Maybe this is fast enough...

transform(a, startOFgene = ifelse(strand == 1, start_position, end_position))


  chromosome_name start_position end_position strand startOFgene
1              15       35574797     35575181      1    35574797
2              15       35590448     35591641     -1    35591641
3              15       35688422     35688645      1    35688422
4              13       75402690     75404217      1    75402690
5              15       35692892     35693969      1    35692892
like image 140
Sven Hohenstein Avatar answered Feb 27 '23 15:02

Sven Hohenstein