Add observations to grids add 0 if not given - left-join

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)

Related

Unrecognized Quartz MS font

I tried to generate the image with the Quartz MS font as follows.
Then use jTessBoxEditorFX to generate the box file, as follows.
0 26 23 97 125 0
1 169 26 189 122 0
2 209 23 279 124 0
3 305 23 370 124 0
4 391 25 461 121 0
5 481 23 551 124 0
6 571 23 641 124 0
7 665 27 731 124 0
8 753 24 822 124 0
9 842 24 912 125 0
The traineddata cannot be recognized normally. Is there any problem with my practice?
Whether there are similar experiences of seniors can guide me, thank you.

Make histogram of pixel intensities without imhist

I have used the unique command to get the unique pixel intensities from my image. Then I tried to make a histogram using them, but it doesn't use all of the intensity values
I = imread('pout.tif');
[rows, columns] = size(I);
UniquePixels=unique(I);
hist=histogram(UniquePixels)
An alternative approach would be to use accumarray combined with unique. I would specifically use the third output of unique to transform your data into a consecutive sequence of 1 up to N where N is the total number of unique intensities, then leverage the first output of unique that will give you the list of unique intensities. Therefore, if the first output of unique is A and the output of accumarray is B, the effect is that at location B(i), this gives the total number of intensities of A(i).
Therefore:
[UniquePixels, ~, id] = unique(I);
histo = accumarray(id, 1);
UniquePixels gives you all unique pixels while histo gives you the counts of each unique pixel corresponding to each element in UniquePixels.
Here's a quick example:
>> I = randi(255, 10, 10)
I =
42 115 28 111 218 107 199 60 140 237
203 22 246 233 159 13 100 91 76 198
80 59 2 47 90 231 62 210 190 125
135 233 198 68 131 241 103 4 49 112
43 39 209 38 103 126 25 11 176 114
154 211 222 35 20 125 34 44 47 79
68 138 22 222 62 87 241 166 94 130
167 255 102 148 32 230 244 187 160 131
176 20 67 141 47 95 147 166 199 209
191 113 205 37 62 29 16 115 21 203
>> [UniquePixels, ~, id] = unique(I);
>> histo = accumarray(id, 1);
>> [UniquePixels histo]
ans =
2 1
4 1
11 1
13 1
16 1
20 2
21 1
22 2
25 1
28 1
29 1
32 1
34 1
35 1
37 1
38 1
39 1
42 1
43 1
44 1
47 3
49 1
59 1
60 1
62 3
67 1
68 2
76 1
79 1
80 1
87 1
90 1
91 1
94 1
95 1
100 1
102 1
103 2
107 1
111 1
112 1
113 1
114 1
115 2
125 2
126 1
130 1
131 2
135 1
138 1
140 1
141 1
147 1
148 1
154 1
159 1
160 1
166 2
167 1
176 2
187 1
190 1
191 1
198 2
199 2
203 2
205 1
209 2
210 1
211 1
218 1
222 2
230 1
231 1
233 2
237 1
241 2
244 1
246 1
255 1
If you double check the input example and the final output, you will see that only the unique pixels are shown combined with their counts. Any bins that were zero in count are not shown.

Creation of a loop loading values from .txt files

i have a problem creating a loop which loads each value from ".txt" files and uses it in some calculations.
All the values are on the 2nd column and the first one is always on the 9th line of each file.
Each ".txt" file contains a different number of values on its 2nd column (they all have the same text after the final value), so i want a loop that can read those values and stop whenever it finds that text)
Here is an example of these files ( the values that interest me are the ones under the headline of G (33,55,93...............,18) )
Latitude: 34°40'30" North,
Longitude: 3°16'6" East
Results for: April
Inclination of plane: 32 deg.
Orientation (azimuth) of plane: 0 deg.
Time G Gd Gc DNI DNIc A Ad Ac
05:52 33 33 25 0 0 233 64 311
06:07 55 44 47 246 361 356 105 473
06:22 93 59 92 312 459 444 124 590
06:37 136 73 147 366 538 514 138 684
06:52 183 86 207 410 602 572 150 760
07:07 232 98 271 447 656 620 160 823
07:22 283 110 337 478 701 659 168 874
16:37 283 110 337 478 701 659 168 874
16:52 232 98 271 447 656 620 160 823
17:07 183 86 207 410 602 572 150 760
17:22 136 73 147 366 538 514 138 684
17:37 93 59 92 312 459 444 124 590
17:52 55 44 47 246 361 356 105 473
18:07 33 33 25 0 0 233 64 311
18:22 18 18 14 0 0 9 8 7
G: Global irradiance on a fixed plane (W/m2)
Gd: Diffuse irradiance on a fixed plane (W/m2)
Gc: Global clear-sky irradiance on a fixed plane (W/m2)
DNI: Direct normal irradiance (W/m2)
DNIc: Clear-sky direct normal irradiance (W/m2)
A: Global irradiance on 2-axis tracking plane (W/m2)
Ad: Diffuse irradiance on 2-axis tracking plane (W/m2)
Ac: Global clear-sky irradiance on 2-axis tracking plane (W/m2)
PVGIS (c) European Communities, 2001-2012

Plot 2D topographic map of EEG node network on MATLAB

I would like to plot a topographic map from EEG network. The electrodes (nodes) have a associated networks metric and from these values I want to interpolate between them and plot in a head shape. Here is the code that i have reseached and the result that I am getting...
=========================================
%The position X and Y as integers (electrodes position) and the value of Z (network metric)
X = [36 51 66 11 22 51 79 91 3 16 51 86 99 1 14 51 88 101 3 16 51 86 99 11 22 51 79 91 36 51 66];
Y = [99 101 99 80 85 87 85 80 66 69 70 69 66 51 51 51 51 51 36 33 32 33 36 22 17 15 17 22 3 1 3];
Z = [-404 -566 -379 -71 -102 -119 -87 9 -62 -160 -104 -81 -26 12 -120 -176 -85 -13 0 -118 -288 -159 -36 -115 -145 -292 -215 -266 -235 -364 -192];
%Making the meshgrid
for dd = 1:31
I(Xd(dd),Yd(dd))=Zd(dd);
end
Zd = [Zd; zeros(70,1)];
Xd = [Xd; zeros(70,1)];
Yd = [Yd; zeros(70,1)];
[XX,YY] = meshgrid(1:101,1:101);
z = griddata(Xd,Yd,Zd,XX,YY,'cubic');
contourf(z)
=========================================
The resulting plot of this code is
http://s16.postimg.org/s7a627s5h/Graph.jpg
I would like some help to remove this "tail" from my graph and a sugggestion of how to draw a head + nose on the same picture (only if it is possible to plot this kind of graph).
I don't have enough reputation to comment above but you can set your own custom locations in EEGlab as far as I remember. Have you looked at the function writelocs? Maybe that helps. EEGlab's topoplots include nose and ears.
http://sccn.ucsd.edu/eeglab/allfunctions/writelocs.html

Multidimensional scaling matrix error

I'm trying to use multidimensional scaling in Matlab. The goal is to convert a similarity matrix to scatter plot (in order to use k-means).
I've got the following test set:
London Stockholm Lisboa Madrid Paris Amsterdam Berlin Prague Rome Dublin
0 569 667 530 141 140 357 396 570 190
569 0 1212 1043 617 446 325 423 787 648
667 1212 0 201 596 768 923 882 714 714
530 1043 201 0 431 608 740 690 516 622
141 617 596 431 0 177 340 337 436 320
140 446 768 608 177 0 218 272 519 302
357 325 923 740 340 218 0 114 472 514
396 423 882 690 337 272 114 0 364 573
569 787 714 516 436 519 472 364 0 755
190 648 714 622 320 302 514 573 755 0
I got this dataset from the book Modern Multidimensional Scaling (Borg & Groenen, 2005). Tested it in SPSS using the PROXSCAL MDS method and I get the same result as stated in the book.
But I need to use MDS in Matlab in order to speed up the process. The tutorial on the site: http://www.mathworks.nl/help/stats/multidimensional-scaling.html#briu08r-4 looks the same as what I'm using above. When I change the data set as what is displayed above and run the code I get the following error: "Not a valid dissimilarity or distance matrix.".
I'm not sure what I'm doing wrong, and if classical MDS is the right choice. I also miss the possibility to say that I want the result in three dimensions (this will be needed in a later stage).
Your matrix is not symetric, check the indices (9,1) and (1,9). To quickly find asymetric indices use [x,y]=find(~(D'==D))