How to subset from a 4 dimensional array in R, based on value -


basically have 4 dimensional array of various radar variables named pol has dim (5,1191,360,11)

for part of analysis want subset analysis pixels below height, note pol[2,,,] contains height information ideally this

newpol<-pol[pol[2,,,]<5,] 

the issue construct not 4 dimensional features, have tried

newpol[,,,]<-pol[,,,][pol[2,,,]<5,,,] 

but results in error "logical subscript long"

my goal obtain 4 dimensional array consisting of elements of pol pol[2,,,]<5.

thanks in advance assistance.

two things here: adding ,drop=false indexing, , which(..., arr.ind=true).

some data:

set.seed(42) pol <- array(sample(20, size=3*4*5*6, replace=true), dim=c(3,4,5,6)) 

this simple subsetting, lose dimensionality c(3,4,5,6) c(4,5,6), not good:

pol[2,,,] < 5 ## , , 1 ##       [,1]  [,2]  [,3]  [,4]  [,5] ## [1,] false false false false false ## [2,] false false false false false ## [3,]  true false false false false ## [4,] false false  true false false ## , , 2 ## ...snip... 

to fix this, use drop=false:

pol[2,,,,drop=false] < 5 ## , , 1, 1 ##       [,1]  [,2] [,3]  [,4] ## [1,] false false true false ## , , 2, 1 ##       [,1]  [,2]  [,3]  [,4] ## [1,] false false false false ## , , 3, 1 ## ...snip... 

a nice trick indices of of "successes" using:

head(which(pol[2,,,,drop=false] < 5, arr.ind=true)) ##      dim1 dim2 dim3 dim4 ## [1,]    1    3    1    1 ## [2,]    1    4    3    1 ## [3,]    1    4    1    2 ## [4,]    1    2    2    2 ## [5,]    1    3    2    2 ## [6,]    1    2    3    2 

to can individual values:

pol[ which(pol[2,,,,drop=false] < 5, arr.ind=true) ] ##  [1] 15 14  5 15 11  2 14 11  4 13  3  9  6  3  4 16 11  7  7 13  8  7 10  8 19 ## [26] 12 

hope helps.


Comments

Popular posts from this blog

javascript - AngularJS custom datepicker directive -

javascript - jQuery date picker - Disable dates after the selection from the first date picker -