0

Not completelty sure if my question is a duplicate...

Suppose I have a dataframe (X), containing 3 columns (A,B and C) with > 100 rows:

X:

A B C
5 10 20
10 8 100
...

Next, I want to use the values of each row (loop) in this dataframe in a script:

For example, in the first "loop"

A<-5
B<-10
C<-20
D<-A*B*C

The second loop

A<-10
B<-8
C<-100
D<-A*B*C

So in the end the ouput of my script contains two values

1000
8000

Anyone could help me with that?

Thank you in advance.

WD

1
  • 2
    Try X$D <- apply(X, 1, prod). Commented Oct 11, 2017 at 22:19

1 Answer 1

1

Here is an option using Reduce

df$D <- Reduce(`*`, df)
Sign up to request clarification or add additional context in comments.

2 Comments

THanks for the proposed solution. But I think I simplified my example too much. I would like to use the variables in each row to make a plot. So for n rows, I would like to make N plots (using the 3 variables).
@WannesDermauw It is better to show something that related to what u r doing

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.