How do I get the nth minus the (n-1)th term from a dataframe in R?

bernie2436

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?

Simon O'Hanlon

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.

edited at
0

Comments

0 comments
Login to comment

Related

How can I get the minimum ratio between of each pair of rows with distance n, for all n from 0 up to the length of the dataframe (minus 1)?

How do I get the nth element of a Seq?

How do I add a column to my dataframe which indicates datemodified minus 1 month of a file in os directory?

How can I get the values at every nth hour from a Pandas DataFrame?

How do I create a new dataframe from every nth row of an existing dataframe while performing math functions on some columns of the first dataframe

How do i remove a specific term in my dataframe string?

How do I get the Nth largest element from a list with duplicates, using LINQ?

How do I turn 'n'% to '0.n' in a dataframe in R

How do I label the nth observation of a specific value in a pandas dataframe?

How can I get the nth largest value in Julia dataframe?

Laravel / PHP how to find the sequence (1th, 2nd, 3d, nth) of a value in collection

in R: how to take value from i+1th row of 1 dataframe and subtract from every row in i+1th column of 2nd dataframe

Woocommerce Wordpress How do I get the Search term as a dynamic variable

How do I get the parent tid of a taxonomy term in Drupal 8

How do i get rid of the term none here and what is it?

How do I get R to recognise a "time" column within a dataframe?

pandas - how do i get the difference from a dataframe on the same column

How do I get a dataframe or database write from TFX BulkInferrer?

How do I get a vocabulary from columns in a dataframe?

How do I get the nth derivative in my scheme program?

How do I remove repeated and identical rows from a dataframe in R?

R: How do I add columns from one dataframe to another?

How to get substring between nth and n+1th occurrence of pipe '|' in shell script

How do I extract the nth occurence of a regex match from a string?

How do I sum the nth number from the right in all cells

How do I get all n! combinations of possible lists that can be created from numbers 1 to n?

How to extract the string from between the Nth and N+1th occurence of a character in Excel 2010?

How to select nth and (n+1)th word from string SQL Server

How to extract string from another string between the Nth and N+1th 'breakword' in php?