Reputation:
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
Reputation: 886938
We can do
(df[-1] - df[-ncol(df)])/df[-ncol(df)]
set.seed(24)
df <- as.data.frame(matrix(1:25, 5, 5))
Upvotes: 1