简体   繁体   English

在给定的开始值和结束值 R 之间提取特定值

[英]extracting specific values between given start and end values R

I have matrix M with n1 number of rows and n2 number of column,i am trying to do is extracting values where its found value greater than or equal to 0.50 and it should take all values until its found value less than 0.50.我有具有 n1 行数和 n2 列数的矩阵 M,我想做的是提取其发现值大于或等于 0.50 的值,并且它应该取所有值,直到其发现值小于 0.50。 and also take 2 values before and after the starting and ending points.I am thinking to start with for loop but I am confused what I should put in condition so that It reads all line of matrix and give output.It would be great if anyone could share any idea.并且还在起点和终点之前和之后取 2 个值。我正在考虑从 for 循环开始,但我很困惑我应该放入什么条件,以便它读取矩阵的所有行并给出输出。如果有人会很棒可以分享任何想法。

Example: M:示例:M:


M<- rbind(c(0.10, 0.20 ,0.40 ,0.50 ,0.49 ,0.52, 0.67, 0.58 ,0.77, 0.34, 0.31, 0.21, 0.87 ,0.65, 0.54, 0.89, 0.78, 0.50, 0.22, 0.34  
),c(0.31, 0.28, 0.74, 0.87 ,0.65, 0.54, 0.78, 0.45, 0.34, 0.41, 0.19 ,0.12 ,0.99, 0.99 ,0.89 ,0.78, 0.50, 0.66 ,0.26, 0.14))

M

  [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] [,15] [,16] [,17] [,18] [,19] [,20]
[1,] 0.10 0.20 0.40 0.50 0.49 0.52 0.67 0.58 0.77  0.34  0.31  0.21  0.87  0.65  0.54  0.89  0.78  0.50  0.22  0.34
[2,] 0.31 0.28 0.74 0.87 0.65 0.54 0.78 0.45 0.34  0.41  0.19  0.12  0.99  0.99  0.89  0.78  0.50  0.66  0.26  0.14

Desired output :期望的输出:

0.20 0.40 0.50 0.49 0.52 0.67 0.58 0.77 0.34 0.31
0.31 0.21 0.87 0.65 0.54 0.89 0.78 0.50 0.22 0.34   
0.31 0.28 0.74 0.87 0.65 0.54 0.78 0.50 0.34 0.41
0.19 0.12 0.99 0.99 0.89 0.78 0.54 0.66 0.26 0.14
for (i in nrows(M))
what should be the condition  here?

What I meant by occurrence : after finishing each occurrence it should be in new line,as you see the after 1st accordance 2nd one is new line我所说的出现是什么意思:完成每次出现后,它应该在新行中,正如你所见,第 1 次之后的第 2 次是新行

1st occurrence 0.20 0.40 0.50 0.49 0.52 0.67 0.58 0.77 0.34 0.31
2nd occurrence 0.31 0.21 0.87 0.65 0.54 0.89 0.78 0.50 0.22 0.34    
3rd occurrence 0.31 0.28 0.74 0.87 0.65 0.54 0.78 0.50 0.34 0.41
4th occurrence 0.19 0.12 0.99 0.99 0.89 0.78 0.54 0.66 0.26 0.14

So I think I have somewhat of an answer.所以我想我有一个答案。 There is several layers to this problem as I understood it.据我了解,这个问题有几个层次。 Besides finding the consecutive occurring numbers above 0.5, there is a need to include values if they are "surrounded" by values > = 0.5, so I have also addressed that.除了找到高于 0.5 的连续出现的数字之外,如果它们被值 > = 0.5“包围”,则需要包括值,所以我也解决了这个问题。 Note that instead of looping over each row, the matrix is turned into one vector (you can also use a loop).请注意,矩阵不是循环遍历每一行,而是变成一个向量(您也可以使用循环)。

#Your data
M<- rbind(c(0.10, 0.20 ,0.40 ,0.50 ,0.49 ,0.52, 0.67, 0.58 ,0.77, 0.34, 0.31, 0.21, 0.87 ,0.65, 0.54, 0.89, 0.78, 0.50, 0.22, 0.34  
),c(0.31, 0.28, 0.74, 0.87 ,0.65, 0.54, 0.78, 0.45, 0.34, 0.41, 0.19 ,0.12 ,0.99, 0.99 ,0.89 ,0.78, 0.50, 0.66 ,0.26, 0.14))

#make the matrix into a single vector, with buffer zeros between rows
m <- cbind(M, rep(c(NA), nrow(M)), rep(c(NA), nrow(M)))
a <- c(t(m))

#make a coresponding binary vecto of the desired values
b <- ifelse(a >= 0.5, 1, 0)

#cumsum that restarts with zeroes
d <- ave(b, cumsum(b == 0), FUN = cumsum)

#patch the holes in desired values: 
#if one value =< 0.5, is surounded by values >= 0.5, consider this value desireble
ind1 <- c(d[-1], 0)
ind2 <- c(0, d[-length(d)])
q <- ifelse((d + ind1 > 0 & d + ind2 > 0), 1, b)

#get start and end indecies for the desired values
z <- with(rle(q), {
  ok <- values == 1 & lengths & !is.na(values) >= 1
  ends <- cumsum(lengths)
  starts <- ends - lengths + 1
  data.frame(starts, ends)[ok, ]
})

## use the indecies to retrive he original values

#define a matrix to put results, using z to create corect dimensions
z$length <- z$ends - z$starts + 5
res <- matrix(NA, nrow(z), ncol = max(z$length))

#forloop to go trough each line of z, creating one line for each output
for (j in 1:nrow(z)) {
  res[ j , 1:z$length[j]] <- a[(z$starts[j]-2):(z$ends[j]+2)]
}

res

PS. PS。 there is a missing value (0.5) from your data, when comparing to your desired output与您想要的输出进行比较时,您的数据中有一个缺失值 (0.5)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM