Dea12
Dea12

Reputation: 55

Add new column to matrix by doing operation between rows

i have this matrix here named Sorted.

     [,1]     [,2]       [,3]       [,4]      
[1,] "a"     "14.06863" "11.50424" "333173.1"
[2,] "b"     "14.50265" "11.89501" "387709.7"
[3,] "c"     "14.55234" "11.95746" "402124"  
[4,] "d"     "14.78606" "12.14149" "453059.3"
[5,] "e"     "15.1697"  "12.51004" "496142.1"
[6,] "f"     "14.41104" "11.81296" "539661.3"
[7,] "g"     "14.86976" "12.23968" "603475.4"

What i need is to add another column, a 5th column which takes the values from subtracting each value of the column 4 with the previous one. It would be like this:

     [,1]     [,2]       [,3]       [,4]        [,5]     
[1,] "a"     "12.06863" "8.50424" "433173.1"   "433173.1 - 0"
[2,] "b"     "12.50265" "8.89501" "487709.7"   "487709.7 - 433173.1 "
[3,] "c"     "12.55234" "8.95746" "502124"     "502124 - 487709.7"
[4,] "d"     "12.78606" "8.14149" "553059.3"   "553059.3 - 502124 "
[5,] "e"     "14.1697"  "8.51004" "596142.1"   "596142.1 - 553059.3"
[6,] "f"     "11.41104" "8.81296" "639661.3"   "639661.3 - 596142.1 "
[7,] "g"     "15.86976" "8.23968" "703475.4"   "703475.4 - 639661.3"

Thanks in advance.

Upvotes: 2

Views: 1223

Answers (3)

smishra
smishra

Reputation: 3418

If matrix name is M then this should do the trick - will create a column diff and set its value from diff of column V4.

M$diff <- M$4
M$diff[2:length(M$V4)] <- diff(M$V4, 1)

Upvotes: 0

Zbynek
Zbynek

Reputation: 5955

First create sample matrix

M <- cbind(letters[1:10], matrix(rnorm(30), ncol=3))

Generate values in new column using sapply

newcol <- c(M[1,4], 
    sapply(2:10, FUN=function(x) {
        # subtract value at x-1 from value at x
        as.numeric(M[as.numeric(x),4])-as.numeric(M[as.numeric(x)-1,4])} ) )

Bind to original matrix

M <- cbind(M, newcol)

Upvotes: 1

jbaums
jbaums

Reputation: 27388

A matrix can only store one type (e.g. character, numeric) of data. Since you have letters in column 1, the entire matrix is coerced to character, and so you can no longer perform arithmetic operations on the data.

A data.frame, on the other hand, only requires that each column contains data of a consistent type, whereas different columns can contain different types of data.

Starting with your matrix, Sorted, try the following:

# Convert the matrix to a data.frame
d <- as.data.frame(Sorted, stringsAsFactors=FALSE)
# Convert all columns except the first to numeric
d[, -1] <- apply(d[, -1], 2, as.numeric)
# Create a new column called diff, which is column 4 minus column 3
d$diff <- d[, 4] - d[, 3]

d

  V1       V2       V3       V4     diff
1  a 14.06863 11.50424 333173.1 333161.6
2  b 14.50265 11.89501 387709.7 387697.8
3  c 14.55234 11.95746 402124.0 402112.0
4  d 14.78606 12.14149 453059.3 453047.2
5  e 15.16970 12.51004 496142.1 496129.6
6  f 14.41104 11.81296 539661.3 539649.5
7  g 14.86976 12.23968 603475.4 603463.2

Edit:

As pointed out in the comments, diff should contain the difference between values in column 4 and their values in previous rows.

A simple way to achieve this is:

d$diff <- c(d[1, 4], diff(d[, 4]))

d
  V1       V2       V3       V4     diff
1  a 14.06863 11.50424 333173.1 333173.1
2  b 14.50265 11.89501 387709.7  54536.6
3  c 14.55234 11.95746 402124.0  14414.3
4  d 14.78606 12.14149 453059.3  50935.3
5  e 15.16970 12.51004 496142.1  43082.8
6  f 14.41104 11.81296 539661.3  43519.2
7  g 14.86976 12.23968 603475.4  63814.1

Upvotes: 1

Related Questions