chrischrischris
chrischrischris

Reputation: 57

How to divide two rows making a third row of the result?

So what I currently have is a dataframe that looks like the following...

rowId   x1    x2    x3    x4
  1     50    40    30    20
  2      5     5     5     5

and what I want is to divide row 1 by row 2 creating a third row with the result, like this...

rowId   x1    x2    x3    x4
  1     50    40    30    20
  2      5     5     5     5
  3     10     8     6     4

what's a simple way of going about doing this?

Upvotes: 1

Views: 2215

Answers (1)

KoenV
KoenV

Reputation: 4283

You could do like this:

df <- read.table(header = TRUE, text = "
x1    x2    x3    x4
50    40    30    20
 5     5     5     5")

df <- rbind ( df, df[1, ] / df[2, ] )

Upvotes: 2

Related Questions