0

This seems like a rudimentary question, but i can't find a solution that would work for me. I have a matrix results.m

> head(results.m)
Perplexity Topics
[1,] 550.8307 2
[2,] 479.3954 3
[3,] 424.5563 4
[4,] 359.7448 5
[5,] 339.7989 6
[6,] 314.3516 7

I don't understand how can i create a column diff that will be equal to the difference between two rows (e.g. for row 2 the value would be -71.4353, and empty in row 1) and a column chg that will be equal to percent change between two rows (13 for row 2).

> data.frame(diff(as.matrix(results.m))) game me an error

Error in r[i1, , drop = FALSE] - r[-nrow(r):-(nrow(r) - lag + 1L), , drop = FALSE] :   non-numeric argument to binary operator

> tail(results.m, -1) - head(results.m, -1) also gave an error

 Error in tail(results.m, -1) - head(results.m, -1) :    non-numeric
 argument to binary operator

What am doing wrong here?

Michael
  • 159
  • 1
  • 2
  • 14
  • Maybe `apply(results.m, 2, diff)`. – Rui Barradas Mar 30 '18 at 18:42
  • @RuiBarradas i get an error when do that `> apply(results.m, 2, diff) Error in r[i1] - r[-length(r):-(length(r) - lag + 1L)] : non-numeric argument to binary operator` – Michael Mar 30 '18 at 23:15
  • Are both columns numeric? It seems that one of them is a `factor`. Maybe `diff(results.m[, 1])`. – Rui Barradas Mar 31 '18 at 08:53
  • @RuiBarradas It appears I had a list instead if a matrix, and following @Michriko's recommendations I have been able to create the `diff` column. Thank you for your help! – Michael Mar 31 '18 at 14:44

1 Answers1

1

you can add a column by cbind it to the existing matrix. so you just have to cbind the difference between the two other columns

results.m = cbind(results.m, c(0,diff(results.m[,1])))

afterwards you can give that column an appropriate name if you want to do so.

colnames(results.m)[3] <- "diff"
Michriko
  • 337
  • 1
  • 11
  • @MKR Yes indeed it is, but I don't get why you blame me, if you answered the question after me? – Michriko Mar 30 '18 at 19:31
  • 1
    Sorry. I dint mean it. Perhaps my window was not refreshed for a while when I added answer. I have realized you have answered before me. Let me add your reference in my answer. – MKR Mar 30 '18 at 19:33
  • okay no problem :) but keep in mind, the question was about matrices not about dataframes – Michriko Mar 30 '18 at 19:41
  • Thank you for your suggestion, but the first line did not work, i get an error `> results.m = cbind(results.m, c(0,diff(results.m[,1]))) Error in r[i1] - r[-length(r):-(length(r) - lag + 1L)] : non-numeric argument to binary operator` – Michael Mar 30 '18 at 19:46
  • My answer doesnt make any sense for matrix. – MKR Mar 30 '18 at 19:46
  • are there any non numeric values in your matrix? I tested the code and it worked for me. what is the output of `str(results.m)` ? – Michriko Mar 30 '18 at 19:51
  • 1
    @Michael Ko the output is `> str(results.m) List of 38 $ : num 551 $ : num 479 $ : num 425 $ : num 360 $ : num 340 $ : num 314 $ : num 305 $ : num 293 $ : num 282 $ : num 271 $ : num 262 $ : num 254 $ : num 245 $ : num 240 $ : num 235 $ : num 225 $ : num 222 $ : num 216 $ : num 213 $ : int 2 .... $ : int 20 - attr(*, "dim")= int [1:2] 19 2 - attr(*, "dimnames")=List of 2 ..$ : NULL ..$ : chr [1:2] "Perplexity" "Topics"` – Michael Mar 30 '18 at 23:10
  • so you don't have a matrix, you have a list. That's why the code does not work. For Matrices the code works. Lists are not that good for calculating. They are more like objects. You can store anything in lists, no matter what type. Please use dataframes or matrices for calculating stuff. You can see here how to transform a list to a matrix: https://stackoverflow.com/questions/13224553/how-to-convert-a-list-to-a-matrix-more-efficiently-in-r – Michriko Mar 31 '18 at 07:31
  • Thank you so much @Michriko, i have been able to transform my list into a matrix and generate the `diff` column. Could you please help me with generating the `chg` column where the `diff` in row `n` is divided by Perplexity in row `n-1`? – Michael Mar 31 '18 at 14:17
  • This is a separate question. Also consider using a dataframe for table like data. The matrix datatype is more for matrices. Also have a look on R tutorials. Those are simple operations on dataframes. – Michriko Apr 01 '18 at 15:11