0

I have a large matrix consisting of 116 columns and 4700 rows.

For each row I would like to compute a standardized difference of the following form:

(((a2-a1)/a1)*100)+100, where a1 is the previous value and a2 is the next value.

In R I'm using the following code:

for i in (1:116)

a[i]=(((a[i]-a[i-1])/a[i-1])*100)+100

However I get the following error:

Error in Ops.data.frame(a[i], a[i - 1]) : 
- only defined for equally-sized data frames

I'm guessing that the problem is that it does not take into account the very first value, where there does not exist a first value-1 to substract.

How can I solve this problem?

Here's a subset of the data: https://dl.dropbox.com/u/22681355/su.csv

Remember that I would like to calculate for each column individually!

user1723765
  • 6,179
  • 18
  • 57
  • 85

1 Answers1

1

Your question could use a reproducible example to make it clearer. For example, you say you have a matrix but then show an error that is related to data frames. Without knowing what your data looks like we can not know if answers are correct.

But anyway, I think you can do this without loops as follows.

# Example data:
foo <- data.frame(A = 1:10, B = rnorm(10))

# Compute standardized differences:
(foo[-1,] - foo[-nrow(foo),]) / foo[-nrow(foo),] * 100 + 100
Community
  • 1
  • 1
Sacha Epskamp
  • 46,463
  • 20
  • 113
  • 131
  • can you please explain what your code is doing? is it taking the difference between the new value and the old value? – user1723765 Nov 15 '12 at 14:18
  • I just take the matrix with the first row removed and substract from that the matrix with the last row removed. Which is essentially the same as subtracting from each element the element above it (previous row). Then I divide using the same trick. – Sacha Epskamp Nov 15 '12 at 14:23
  • I've added a subset of the data. Just to make sure: you take the second value in the row and substract the first value then divide by the first value and so on right? – user1723765 Nov 15 '12 at 14:24
  • Yes, but per column. So element `[1,1]` of the output is `(foo[1,2] - foo[1,1]) / foo[1,1] * 100 + 100`. If you want do do it rowise you can just change the commas and `nrow` to `ncol`. e.g. `foo[-nrow(foo),]` then becomes `foo[,-ncol(foo)]` – Sacha Epskamp Nov 15 '12 at 14:31
  • great, one last thing: if I want to do the same but instead of always substracting the previous values, always substracting the very first value how can I do that? do I do foo-foo[,1] ? – user1723765 Nov 15 '12 at 14:36
  • Depends on if its rowwise or columnwise. Since `foo[,1]` is smaller than `foo` it is repeated, columnwise. So this would work if you want to substract the first column from all columns (`foo - foo[,1]`) but not if you want to substract the first row (`foo - foo[1,]`). – Sacha Epskamp Nov 15 '12 at 14:49
  • I want to substract the first row of each column from all other values of the respective column – user1723765 Nov 15 '12 at 14:50
  • Then you can try `df <- read.csv("su.csv",row.names=NULL);sapply(df,function(x)x-x[1])`. – Sacha Epskamp Nov 15 '12 at 14:53
  • right and how do I add the dividing and multiplying part from the original code? – user1723765 Nov 15 '12 at 14:57
  • `sapply(df,function(x)(x-x[1])/x[1] * 100 + 100)` – Sacha Epskamp Nov 15 '12 at 15:02