'Add observations to grids add 0 if not given
I followed the following post: Aggregate values in raster using SF
points <- st_as_sf(SPDF_Violence["dummy"], coords = c("longitude", "latitude"),
proj4string =CRS("+init=epsg:4326 +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84"))
pointsID <- st_join(grid, points)
pointsID <- pointsID %>%
as.data.frame() %>%
group_by(ID) %>%
summarize(best1 = sum(dummy))
A<- left_join(grid, pointsID, by = "ID")
This is the code I use to join the points to the grids. My Question is now, how can I add the value of 0 to those grids, where I don't have any observations in my Dataframe A?
Thanks for your help! (sorry for not providing any reproducible Data, but I just don't know how should publish df big like that: maybe the structure of the Datasets helps:
s
structure(grid)
Simple feature collection with 579 features and 13 fields
Geometry type: GEOMETRY
Dimension: XY
Bounding box: xmin: 33.00154 ymin: 3.398829 xmax: 47.95823 ymax: 14.84548
CRS: +proj=longlat +proj +ellps=WGS84 +datum=WGS84 +no_defs
First 10 features:
ID ID_0 ISO NAME_0 ID_1 NAME_1 HASC_1 CCN_1 CCA_1 TYPE_1 ENGTYPE_1 NL_NAME_1
312 312 74 ETH Ethiopia 1 Addis Abeba ET.AA 0 14 Astedader City <NA>
342 342 74 ETH Ethiopia 1 Addis Abeba ET.AA 0 14 Astedader City <NA>
315 315 74 ETH Ethiopia 2 Afar ET.AF 0 02 Kilil State <NA>
344 344 74 ETH Ethiopia 2 Afar ET.AF 0 02 Kilil State <NA>
345 345 74 ETH Ethiopia 2 Afar ET.AF 0 02 Kilil State <NA>
346 346 74 ETH Ethiopia 2 Afar ET.AF 0 02 Kilil State <NA>
374 374 74 ETH Ethiopia 2 Afar ET.AF 0 02 Kilil State <NA>
375 375 74 ETH Ethiopia 2 Afar ET.AF 0 02 Kilil State <NA>
376 376 74 ETH Ethiopia 2 Afar ET.AF 0 02 Kilil State <NA>
404 404 74 ETH Ethiopia 2 Afar ET.AF 0 02 Kilil State <NA>
VARNAME_1 geometry
312 Āddīs Ābaba|Addis Ababa|Adis-Abeba|Ādīs Ābeba POLYGON ((38.8761 8.898886,...
342 Āddīs Ābaba|Addis Ababa|Adis-Abeba|Ādīs Ābeba POLYGON ((38.77125 9.098195...
315 <NA> POLYGON ((40.12328 8.898884...
344 <NA> POLYGON ((40.00154 9.006727...
345 <NA> POLYGON ((40.50154 9.376003...
346 <NA> POLYGON ((40.64428 9.398895...
374 <NA> MULTIPOLYGON (((39.87534 9....
375 <NA> POLYGON ((40.00154 9.732064...
376 <NA> POLYGON ((40.96569 9.898848...
404 <NA> POLYGON ((40.00154 9.9927, ...
structure(points)
Simple feature collection with 59 features and 1 field
Geometry type: POINT
Dimension: XY
Bounding box: xmin: 34.53333 ymin: 3.536615 xmax: 44.27806 ymax: 12.41674
CRS: +proj=longlat +datum=WGS84 +no_defs
# A tibble: 59 x 2
dummy geometry
<dbl> <POINT [°]>
1 1 (43.55722 8.220556)
2 1 (39.0531 3.536615)
3 1 (35.59728 9.370403)
4 1 (38.08333 4.883333)
5 1 (38.6 7.2)
6 1 (42.08132 9.33658)
7 1 (39.59622 11.82968)
8 1 (37.85 8.98333)
9 1 (41.56037 9.397441)
10 1 (38.95 8.3)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
