1

I'm running a loop in which two numeric columns are created based on some data. These two column names are generated by the part of the loop they are in. So for example, the first two columns are df$loop1.1 and df$loop1.2 and then in the following loop they'd be df$loop2.1 and df$loop2.2

I need to create a third column that is based off of those two columns like so:

df[, divisioncol := loop1.1 / loop1.2]

However, I'm getting the error

    non-numeric argument to binary operator

Any suggestions?

Solved:

 df[, divisioncol := get(loop1.1)/get(loop2.2)]
Justin
  • 153
  • 2
  • 10
  • 1
    If you are passing the column names, maybe wrap in `get`, like `get(loop1.1) / get(loop1.2)` https://stackoverflow.com/q/12391950/ – Frank Mar 25 '19 at 20:58

1 Answers1

0

Your error is caused by a non-numeric argument to a binary operator. The binary operator here is the function /, which is division. This means that either loop1.1 or loop1.2 contains a non-numeric value. Verify this by typing typeof(df$loop1.1) and typeof(df$loop1.2)

Corey Levinson
  • 1,553
  • 17
  • 25