Is there any code to choose all elements of a matrix between interval (the interval are: min(data[,1])
and min(dat开发者_运维技巧a[,dim(data)[2]]))
? For example, the data is like this:
> data <- matrix(c(58,47,40,42,38,22,53,43,36,62,51,44),byrow=T,ncol=3)
The chosen elements should be: 22,36,38,40,42.
Many thanks in advance.Given that you you want all elements between the minimum of the first column and the minimum of the last colum, you can index the matrix directly:
dat <- matrix(c(58, 47, 40, 42, 38, 22, 53, 43, 36, 62, 51, 44), byrow = TRUE, ncol = 3)
## grab the two values and sort them (assumes there are no missing values)
## using ncol() is a bit neater than dim(x)[2] for a matrix
minmax <- sort(c(min(dat[,1]), min(dat[,ncol(dat)])))
## subset by direct indexing (as if dat were a vector)
res <- dat[dat >= minmax[1] & dat <= minmax[2]]
## sort the result
sort(res)
[1] 22 36 38 40 42
I called my matrix "dat" rather than "data", as that is a function in R.
精彩评论