mardi 29 septembre 2020

IFELSE does not recognize negative numbers

I have a dataframe with latitude and longitude. I have an other dataframe with the code of each 5x5 grid, and a column with the lat max and min and lon max and min for each grid. I want to create a new column in my first dataframe, called "cuad55", using the limits for each grid from the dataframe B, that is, I want to assing the grid code from my B dataframe, to my A dataframe, using lat y lon of A and limits from B:

sample of dataframe A (CALLED "plantados_totales"):

POINT || LATITUDE || LONGITUDE
1        0.933333     49.3667
2        4.250000     52.1500
3       -49.83333     49.8333
4       -34.35000     53.3667

sample of dataframe B (CALLED "cuadrícula"):

CUADRÍCULA || LAT MIN || LAT MAX || LON MIN || LON MAX
100045           0           5         45         50
100050           0           5         50         55
245045          -50         -45        45         50
230050          -35         -30        50         55 

I executed this code:

plantados_totales$cuad55 <- ifelse((plantados_totales$latitude > cuadricula$latmin & plantados_totales$latitude < cuadricula$latmax & plantados_totales$longitude > cuadricula$lonmin & plantados_totales$longitude < cuadricula$lonmax ), cuadricula$cuadricula, "NA")

But when I check the result, R only assign the correct grid code when the latitude is positive. When it's negative, R assign "NA":

POINT || LATITUDE || LONGITUDE || cuad55
1        0.933333     49.3667     100045
2        4.250000     52.1500     100050
3       -49.83333     49.8333       NA
4       -34.35000     53.3667       NA

All my data are in numeric format... does anyone have any idea what may be going on?

Thank you in advance!! Regards!

Aucun commentaire:

Enregistrer un commentaire