user4679594
user4679594

Reputation:

Apply function across rows

Is there a more efficient way to do this?

I have a df with 4 rows and 25 columns, and I want to make a calculation across all rows with the following formula:

(df['next column'] - df['current column'])/df['current column']

Upvotes: 1

Views: 66

Answers (1)

akrun
akrun

Reputation: 886938

We can do

(df[-1] - df[-ncol(df)])/df[-ncol(df)]

data

set.seed(24)
df <- as.data.frame(matrix(1:25, 5, 5))

Upvotes: 1

Related Questions