I have a dataframe that is n rows and 1 column. I can do this to find the 2nd row minus the 1st row:
> dif = df[2,1] - df[1,1]
How do I find df[n,1] - df[n-1, 1]
for all rows in df?
head
and tail
are handy for this...
df <- data.frame( a = 1:5 , b = 5:1 )
tail(df,-1) - head(df,-1)
# a b
#2 1 -1
#3 1 -1
#4 1 -1
#5 1 -1
This will therefore accomplish what you are after, row by row, for all columns at the same time.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments