热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

如何在满足某些条件后调用矩阵中的行?-Howtocalltherowinamatrixaftercertaincriteriahasbeenmet?

Ihaveamatrixcalledtotal_warnings我有一个名为total_warnings的矩阵rowdayTxHxTn3636

I have a matrix called total_warnings

我有一个名为total_warnings的矩阵

     row day  Tx    Hx   Tn
36   36   5 32.5 37.78 16.3
41   41  10 33.3 41.46 20.5
42   42  11 31.7 41.57 21.2
43   43  12 31.3 39.03 22.2
45   45  14 29.1 37.34 19.0
55   55  24 33.7 41.77 16.4
56   56  25 29.3 38.07 15.9
58   58  27 35.0 41.07 15.3
59   59  28 34.3 40.37 18.5
61   61  30 33.5 40.34 14.9
65   65   4 31.0 37.24 11.4
72   72  11 32.6 40.00 16.2
73   73  12 33.8 40.25 16.8
74   74  13 34.7 41.04 18.6
76   76  15 31.4 39.47 18.4
77   77  16 27.7 37.30 18.4
78   78  17 27.9 38.36 22.3
79   79  18 32.9 42.07 20.1
82   82  21 32.0 38.62 19.3
93   93   1 28.6 37.55 18.1 
94   94   2 30.3 39.71 15.3
95   95   3 32.7 39.46 17.5
101 101   9 33.0 39.00 17.4
104 104  12 27.5 37.10 16.7
135 135  12 32.6 38.45 17.7

What I'm attempting to do is indentify the contents of the row ONE AFTER three or more consecutive rows. For example 41, 42, 43 are three consecutive rows, I would like to call the contents of row 45!

我试图做的是在三个或更多连续行之后识别行ONE的内容。例如41,42,43是连续三行,我想调用第45行的内容!

45  14 29.1 37.34 19.0

So for this example rows 45, 82, 101 would be returned. If the row after the three or more consecutive rows is part of another set of TWO or more consecutive rows I DO NOT need to know the contents. For example 72, 73, 74 is a set but I do not want row 76 because it is part of another set 76, 77, 78, 79!

因此,对于该示例,将返回行45,82,101。如果三个或更多连续行之后的行是另一组两个或更多个连续行的一部分,则我不需要知道内容。例如72,73,74是一组,但我不想要第76行,因为它是另一组76,77,78,79的一部分!

Thanks for the help in advance! Nick

我在这里先向您的帮助表示感谢!缺口

1 个解决方案

#1


1  

The rle function (run length encoding) can be combined with the diff function to identify the locations where the differences are 1 and have length greater than or equal to 2. The diff-operation actually shifts the names by 1 so there's no need to add 1 to the names()-result. That character result can then be used to index a matrix with rownames:

rle函数(运行长度编码)可以与diff函数结合使用,以识别差异为1且长度大于或等于2的位置。差异操作实际上将名称移动1,因此无需添加1到名字() - 结果。然后可以使用该字符结果来索引具有rownames的矩阵:

> rle( diff(mat[,'row']))
Run Length Encoding
  lengths: Named int [1:19] 1 2 1 1 1 1 1 1 1 1 ...
 - attr(*, "names")= chr [1:19] "42" "45" "55" "56" ...
  values : Named num [1:19] 5 1 2 10 1 2 1 2 4 7 ...
 - attr(*, "names")= chr [1:19] "41" "43" "45" "55" ...
> rdiffs <- rle( diff(mat[,'row']))
> rdiffs$lengths >= 2
   42    45    55    56    58    59    61    65    72    73    76    77 
FALSE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE  TRUE FALSE 
   82    93    94   101   104   135       
 TRUE FALSE FALSE  TRUE FALSE FALSE FALSE 

> attr(rdiffs$lengths,"names")[ rdiffs$lengths >= 2 ]
[1] "45"  "76"  "82"  "101"

> mat [ attr(rdiffs$lengths,"names")[ rdiffs$lengths >= 2 & rdiffs$values == 1] , ]
    row day   Tx    Hx   Tn
45   45  14 29.1 37.34 19.0
76   76  15 31.4 39.47 18.4
82   82  21 32.0 38.62 19.3
101 101   9 33.0 39.00 17.4

I'll also put in a reproducible example:

我还将提供一个可重现的例子:

dn <- scan()
36   36   5 32.5 37.78 16.3
41   41  10 33.3 41.46 20.5
42   42  11 31.7 41.57 21.2
43   43  12 31.3 39.03 22.2
45   45  14 29.1 37.34 19.0
55   55  24 33.7 41.77 16.4
56   56  25 29.3 38.07 15.9
58   58  27 35.0 41.07 15.3
59   59  28 34.3 40.37 18.5
61   61  30 33.5 40.34 14.9
65   65   4 31.0 37.24 11.4
72   72  11 32.6 40.00 16.2
73   73  12 33.8 40.25 16.8
74   74  13 34.7 41.04 18.6
76   76  15 31.4 39.47 18.4
77   77  16 27.7 37.30 18.4
78   78  17 27.9 38.36 22.3
79   79  18 32.9 42.07 20.1
82   82  21 32.0 38.62 19.3
93   93   1 28.6 37.55 18.1 
94   94   2 30.3 39.71 15.3
95   95   3 32.7 39.46 17.5
101 101   9 33.0 39.00 17.4
104 104  12 27.5 37.10 16.7
135 135  12 32.6 38.45 17.7
mat <- matrix(dn, ncol=6, byrow=TRUE)
mat<- mat[, -1]
colnames(mat) <- c('row', 'day',  'Tx',    'Hx',   'Tn')
rownames(mat) <-mat[,'row']

推荐阅读
author-avatar
祥荣华贵
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有