包含原始行元素之间差异的新矩阵 [英] New matrix containing difference between row elements of original

查看:94
本文介绍了包含原始行元素之间差异的新矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获取一个现有的MxN矩阵并创建一个新的M-1xN矩阵,以使每一列的元素都是原始矩阵的相邻行元素之间的差.

I want to take an existing MxN matrix and create a new M-1xN matrix such that for each columns, the elements are the difference between adjacent row elements of the original matrix.

想法是数据从累积类型变为费率类型...

The idea is the data goes from a cumulative type to a rate type...

例如: 我有(每列是一个特定的数据系列).

eg: I have (where each column is a specific data series).

   [,1]  [,2] [,3] 
[1,] "17"  "16" "15" 
[2,] "34"  "32" "32" 
[3,] "53"  "47" "48"  
[4,] "72"  "62" "63" 
[5,] "90"  "78" "79"  
[6,] "109" "94" "96"  

我想要-

   [,1]  [,2] [,3] 
[1,] "17"  "16" "17"  
[2,] "19"  "15" "16" 
[3,] "19"  "15" "15" 
[4,] "18"  "16" "16" 
[5,] "19"  "16" "17" 

推荐答案

对于数字数据非常简单(不确定为什么要使用字符):

It's very simple for numerical data (not sure why you have characters):

diff(m)

使用字符数据,这应该可以工作:

With the character data, this should work:

diff(matrix(as.numeric(m), dim(m)))

这篇关于包含原始行元素之间差异的新矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆