我有两列这样的值:

>bb
          GDis      BDis
    1   12.291488  8.009909
    2   11.283319 13.625103
    3   6.674549  8.629232
    4   13.493121 17.175888
    5   9.550731  9.867878
    6   9.193895  9.785301
    7   10.541702 10.941371
    8   9.849527  9.496284
    9   8.682287  8.133774
    10  8.439381  4.335260

我需要添加额外的列,并将其命名为Index,如果GDis/BDis较大,它将计算比率GDis;如果BDis/GDis较大,则会计算BDis

我怎么做?

最佳答案

尝试

 transform(bb, Index=ifelse(GDis>BDis, GDis/BDis, BDis/GDis))
 #        GDis      BDis      adn
 #1  12.291488  8.009909 1.534535
 #2  11.283319 13.625103 1.207544
 #3   6.674549  8.629232 1.292856
 #4  13.493121 17.175888 1.272937
 #5   9.550731  9.867878 1.033207
 #6   9.193895  9.785301 1.064326
 #7  10.541702 10.941371 1.037913
 #8   9.849527  9.496284 1.037198
 #9   8.682287  8.133774 1.067436
 #10  8.439381  4.335260 1.946684

10-06 06:39