Wannes Dermauw
Wannes Dermauw

Reputation: 161

loop over variables in dataframe R

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

Upvotes: 0

Views: 280

Answers (1)

akrun
akrun

Reputation: 886938

Here is an option using Reduce

df$D <- Reduce(`*`, df)

Upvotes: 1

Related Questions