Question

I need to subset a matrix and retain the results as a matrix i.e i want to subset all colums with values between 2 and 8 in 2 different matrices and then subset a third matrix by the result. the first two matrices are latitude and longitude values. I tried it this 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 I tried was

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

THis did the subsetting of all the cell numbers but i want it retained as a grid or matrix of rows and columns as it returned a linear array . I need it to retain just the rows and columns that have these values as a matrix so I can subset another matrix 'Data' that has the actual values in so I can then subset using .

New<-Data[Newlon,Newlat]

The file is available here: Link

The files where taken out of an 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 do I go about this?

Was it helpful?

Solution

Here is some 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 # which is the same as lat in this 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 and digits=1 here for making the example easy to look 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 is not entirely clear what the desired output should be. If I understand the question correctly, OP wants to keep values from Data where the corresponding lon and lat values fall between 2 and 8. The form of the output is what I'm unclear about. This answer keeps a matrix and turns all values that do not meet the criteria to 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, I would probably do something more like this, but OP explicitly mentions working with and returning matrices:

# need to redefine Data from 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,]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top