r - Subset a matrix by element and keep values as a matrix -


i need subset matrix , retain results matrix i.e want subset colums values between 2 , 8 in 2 different matrices , subset third matrix result. first 2 matrices latitude , longitude values. tried way:

lond<-c(2,9) latd<-c(2,9) newlon <-which(lon > lond[1] & lon < lond[2]) newlat <-which(lat > latd[1] & lat < latd[2]) 

another option tried was

index <- which(lat >= 2 & lat <= 9 & lon >= 2 & lon <= 9) 

this did subsetting of cell numbers want retained grid or matrix of rows , columns returned linear array . need retain rows , columns have these values matrix can subset matrix 'data' has actual values in can subset using .

new<-data[newlon,newlat] 

the file available here: link

the files taken out of hdf5 file using rhdf5 thus

lon<-h5read ("m1.he5","hdfeos/swaths/columnamountno2/geolocation fields/longitude") lat<-h5read ("m1.he5","hdfeos/swaths/columnamountno2/geolocation fields/latitude") data<-h5read ("m1.he5","hdfeos/swaths/columnamountno2/data fields/columnamountno2trop") 

how go this?

here sample data:

lon <- matrix(seq(-10,9.8,by=0.2),ncol=10,byrow=t) lat <- matrix(seq(-10,9.8,by=0.2),ncol=10,byrow=t) lon # same lat in example:       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]  [1,]  -10 -9.8 -9.6 -9.4 -9.2   -9 -8.8 -8.6 -8.4  -8.2  [2,]   -8 -7.8 -7.6 -7.4 -7.2   -7 -6.8 -6.6 -6.4  -6.2  [3,]   -6 -5.8 -5.6 -5.4 -5.2   -5 -4.8 -4.6 -4.4  -4.2  [4,]   -4 -3.8 -3.6 -3.4 -3.2   -3 -2.8 -2.6 -2.4  -2.2  [5,]   -2 -1.8 -1.6 -1.4 -1.2   -1 -0.8 -0.6 -0.4  -0.2  [6,]    0  0.2  0.4  0.6  0.8    1  1.2  1.4  1.6   1.8  [7,]    2  2.2  2.4  2.6  2.8    3  3.2  3.4  3.6   3.8  [8,]    4  4.2  4.4  4.6  4.8    5  5.2  5.4  5.6   5.8  [9,]    6  6.2  6.4  6.6  6.8    7  7.2  7.4  7.6   7.8 [10,]    8  8.2  8.4  8.6  8.8    9  9.2  9.4  9.6   9.8  set.seed(1) # i'm using print , digits=1 here making example easy at. data <- print(matrix(runif(100, 400, 500),ncol=10),digits=1); data        [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]  [1,]  427  421  493  448  482  448  491  434  443   424  [2,]  437  418  421  460  465  486  429  484  471   406  [3,]  457  469  465  449  478  444  446  435  440   464  [4,]  491  438  413  419  455  424  433  433  433   488  [5,]  420  477  427  483  453  407  465  448  476   478  [6,]  490  450  439  467  479  410  426  489  420   480  [7,]  494  472  401  479  402  432  448  486  471   446  [8,]  466  499  438  411  448  452  477  439  412   441  [9,]  463  438  487  472  473  466  408  478  425   481 [10,]  406  478  434  441  469  441  488  496  414   460 

it not entirely clear desired output should be. if understand question correctly, op wants keep values data corresponding lon , lat values fall between 2 , 8. form of output i'm unclear about. answer keeps matrix , turns values not meet criteria na:

data[!(lon > 2 & lon < 8)] <- na data[!(lat > 2 & lat < 8)] <- na print(data, digits=1)         [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]  [1,]   na   na   na   na   na   na   na   na   na    na  [2,]   na   na   na   na   na   na   na   na   na    na  [3,]   na   na   na   na   na   na   na   na   na    na  [4,]   na   na   na   na   na   na   na   na   na    na  [5,]   na   na   na   na   na   na   na   na   na    na  [6,]   na   na   na   na   na   na   na   na   na    na  [7,]   na  472  401  479  402  432  448  486  471   446  [8,]  466  499  438  411  448  452  477  439  412   441  [9,]  463  438  487  472  473  466  408  478  425   481 [10,]   na   na   na   na   na   na   na   na   na    na 

personally, more this, op explicitly mentions working , returning matrices:

# need redefine data above.  set.seed(1);data <- matrix(runif(100, 400, 500),ncol=10) newdat <- cbind(lon = as.vector(lon),                 lat = as.vector(lat),                 data = as.vector(data))  newdat[lon>2 & lon<8 & lat>2 & lat<8,] 

Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

How to get multiresult with multicondition in Sql Server -