[英]Extract values at row r and column n-1 in matrix in R
I'd like to return a list of some values within my matrix.我想返回矩阵中一些值的列表。 Essentially I would be looking to extract values at
本质上,我希望在以下位置提取值
[2,1]
[5,4]
[8,7]
and so on.
Basically, every third row beginning at row n=2
, and the corresponding value in column n-1
.基本上,从第
n=2
行开始的每第三行,以及第n-1
列中的相应值。
It seems simple, but I can't wrap my head around it.这看起来很简单,但我无法理解它。
My matrix is 141x141
.我的矩阵是
141x141
。 This output should yield 47 values.此输出应产生 47 个值。
You can try this:你可以试试这个:
m <- matrix(1:100, ncol = 10, byrow = T) # some toy data
s <- seq(2, nrow(m), 3)
m[cbind(s,s-1)]
#[1] 11 44 77
m
# [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
# [1,] 1 2 3 4 5 6 7 8 9 10
# [2,] 11 12 13 14 15 16 17 18 19 20
# [3,] 21 22 23 24 25 26 27 28 29 30
# [4,] 31 32 33 34 35 36 37 38 39 40
# [5,] 41 42 43 44 45 46 47 48 49 50
# [6,] 51 52 53 54 55 56 57 58 59 60
# [7,] 61 62 63 64 65 66 67 68 69 70
# [8,] 71 72 73 74 75 76 77 78 79 80
# [9,] 81 82 83 84 85 86 87 88 89 90
#[10,] 91 92 93 94 95 96 97 98 99 100
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.