Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201839

Conditionally sum a range of columns based on another column in R

$
0
0

I've looked through several other related questions but haven't really found something that meets my case.

I have a column that dictates how many columns will be summed into a new column.

  • If DEPENDENCY == INDEP then NET_AGI = IND_AGI
  • If DEPENDENCY == DEP then NET_AGI = PRO_AGI + IND_AGI
  • Otherwise NET_AGI = PRO_AGI
DEPENDENCY IND_AGI PRO_AGI  NET_AGI <- NET_AGI will be the summed column
INDEP      0049995    -     0049995
DEP        0000500 0090500  0091000
DEP        0009000 0121095  0130950
DEP           -    0375001  0375001
INDEP      0123456    -     0123456
DEP        0012070 1023030  1035100
...

What's the best way to do that?


Viewing all articles
Browse latest Browse all 201839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>