避免在R中逐行处理data.frame

vin*_*h85 4 performance r dataframe

我想知道在R中避免行方式处理的最佳方法是什么,大多数行方式都是在内部C例程中完成的.例如:我有一个数据框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
Run Code Online (Sandbox Code Playgroud)

我想要的是:基于链是正还是负,startOFgenestart_positionend_position.避免for循环的一种方法是将data.frame与+1 strand和-1 strand分开并执行选择.什么可以加快速度?如果每行具有某些其他复杂处理,则该方法不会按比例放大.

Sve*_*ein 5

也许这足够快......

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
Run Code Online (Sandbox Code Playgroud)