通过在行之间执行操作将新列添加到矩阵

时间:2014-02-19 09:42:03

标签: r

我这个矩阵名为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"

我需要的是添加另一列,第5列,它使用前一个列减去第4列的每个值。它会是这样的:

     [,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"

提前致谢。

3 个答案:

答案 0 :(得分:1)

matrix只能存储一种类型(例如字符,数字)的数据。由于第1列中有字母,因此整个矩阵都被强制转换为字符,因此您无法再对数据执行算术运算。

另一方面,data.frame只要求每个包含一致类型的数据,而不同的列可以包含不同类型的数据。

从您的矩阵Sorted开始,尝试以下操作:

# 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

修改

正如评论中所指出的,diff应该包含第4列中的值与前一行中的值之间的差异。

实现这一目标的一个简单方法是:

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

答案 1 :(得分:1)

首先创建样本矩阵

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

使用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])} ) )

绑定到原始矩阵

M <- cbind(M, newcol)

答案 2 :(得分:0)

如果矩阵名称是M,那么这应该是技巧 - 将创建一个列差异,并从列V4的差异中设置其值。

M $ diff&lt; - M $ 4
M $ diff [2:长度(M $ V4)]&lt; - 差异(M $ V4,1)