Suppose I have a sample data.frame
as follows:
1 2 3 4 5 6 7 8 9 11
2 3 4 5 6 7 8 9 10 12
3 4 5 6 7 8 9 10 11 13
I want to subtract each column from its next column in my data.frame
, so I get something like this:
1 1 1 1 1 1 1 1 2
1 1 1 1 1 1 1 1 2
1 1 1 1 1 1 1 1 2
My real sample has 2291 rows and 50 columns, the code I wrote looks something like this:
delta <- data.frame(matrix(NA, nrow = 2291, ncol = 49 ))
for(i in 1:nrow(df)){
for(j in 1:ncol(df)-1){
delta[i,j] <- df[i,j+1]-df[i,j]
}
}
But I get the following error message:
Error in data.frame(value, row.names = rn, check.names = FALSE, check.rows = FALSE) : 'row.names' should specify one of the variables
What am I doing wrong? What is the best way to do this?