pklemmer
pklemmer

Reputation: 1

How would I repeat a function over each row when I need to specify the columns of the corresponding data frame in the function?

The ms.compute function in the RxnSim package requires me to specify two variables I want to compare. I am not sure how I can define the variables (i.e. ms.compute(x,y)) in the ms.compute function so that the function uses whatever is in the columns in the current row, and make the function repeat for each row, and ideally output the result in a third column.

      df
      X1 X2
    1  1  6
    2  2  7
    3  3  8
    4  4  9
    5  5 10

For this df, I would want a third column X3 to return the result of a function like: FUN(x+y), in which x,y are 1,6 for row 1 etc. and have this function repeat for each row, so row 1 = 7, row 2 = 9 etc.

Upvotes: 0

Views: 83

Answers (1)

ale_ish
ale_ish

Reputation: 159

Using dplyr might be easier

df <- df%>%
mutate(sum = x1+x2)

Upvotes: 1

Related Questions