How to Convert CSV to Raster in R? - r

I have a CSV (value, carbon, latitude, longitude) that I am trying to create a raster from.
CSV file sample:
Carbon Latitude Longitude coords.x1 coords.x2
1 385 36 74 36 74
2 463 36 74 36 74
3 35 36 74 36 74
4 38 36 74 36 74
5 34 36 74 36 74
6 11 36 74 36 74
7 46 36 74 36 74
8 18 36 74 36 74
9 213 36 74 36 74
10 619 36 74 36 74
11 140 36 74 36 74
12 40 36 74 36 74
13 42 36 74 36 74
14 18 36 74 36 74
15 277 36 74 36 74
16 641 36 74 36 74
17 416 36 74 36 74
18 459 36 74 36 74
19 1073 36 74 36 74
20 628 36 74 36 74
21 425 36 74 36 74
22 550 36 74 36 74
23 163 36 74 36 74
24 366 36 74 36 74
25 379 36 74 36 74
26 279 36 74 36 74
27 284 36 74 36 74
28 454 36 74 36 74
29 813 36 74 36 74
30 1296 36 74 36 74
31 1539 36 74 36 74
32 997 36 74 36 74
33 498 36 74 36 74
34 857 36 74 36 74
35 413 36 74 36 74
36 76 36 74 36 74
37 189 36 74 36 74
38 130 36 74 36 74
39 22 36 74 36 74
40 18 36 74 36 74
41 137 36 74 36 74
42 521 36 74 36 74
43 28 36 74 36 74
44 188 36 74 36 74
45 101 36 74 36 74
46 19 36 74 36 74
47 935 36 74 36 74
48 22 36 74 36 74
49 22 36 74 36 74
50 165 36 74 36 74
51 274 36 74 36 74
52 316 36 74 36 74
53 270 36 74 36 74
54 125 36 74 36 74
55 116 36 74 36 74
56 109 36 74 36 74
57 70 36 74 36 74
58 194 36 74 36 74
59 36 36 74 36 74
60 24 36 74 36 74
61 93 36 74 36 74
62 32 36 74 36 74
63 144 36 74 36 74
64 47 36 74 36 74
65 304 36 74 36 74
66 338 36 74 36 74
67 214 36 74 36 74
68 150 36 74 36 74
69 1799 36 74 36 74
70 394 36 74 36 74
71 24 36 74 36 74
72 117 36 74 36 74
73 140 36 74 36 74
74 47 36 74 36 74
75 3 36 74 36 74
76 221 36 74 36 74
77 41 36 74 36 74
78 319 36 74 36 74
79 119 36 74 36 74
80 39 36 74 36 74
81 3 36 74 36 74
82 2 36 74 36 74
83 15 36 74 36 74
84 69 36 74 36 74
85 40 36 74 36 74
86 233 36 74 36 74
87 15 36 74 36 74
88 147 36 74 36 74
89 50 36 74 36 74
90 348 36 74 36 74
91 549 36 74 36 74
92 5 36 74 36 74
93 191 36 74 36 74
94 409 36 75 36 75
95 93 36 75 36 75
96 1641 36 75 36 75
97 154 36 75 36 75
98 852 36 75 36 75
99 1571 36 75 36 75
100 1173 36 75 36 75
101 19 36 75 36 75
102 9 36 75 36 75
103 15 36 75 36 75
104 67 36 75 36 75
105 666 36 75 36 75
106 3 36 75 36 75
107 227 36 75 36 75
108 130 36 75 36 75
109 423 36 75 36 75
110 31 36 75 36 75
111 559 36 75 36 75
112 143 36 75 36 75
113 63 36 75 36 75
114 1211 36 75 36 75
115 280 36 75 36 75
116 1027 36 75 36 75
117 636 36 75 36 75
118 207 36 75 36 75
119 233 36 75 36 75
120 332 36 75 36 75
121 266 36 75 36 75
122 266 36 75 36 75
123 284 36 75 36 75
124 240 36 75 36 75
125 613 36 75 36 75
126 28 36 75 36 75
127 762 36 75 36 75
128 58 36 75 36 75
129 310 36 75 36 75
130 12 36 75 36 75
131 15 36 75 36 75
132 343 36 75 36 75
133 128 36 75 36 75
134 177 36 75 36 75
135 320 36 75 36 75
136 205 36 75 36 75
137 108 36 75 36 75
138 1445 36 75 36 75
139 109 36 75 36 75
140 251 36 75 36 75
141 262 36 75 36 75
142 282 36 75 36 75
143 188 36 75 36 75
144 207 36 75 36 75
145 63 36 75 36 75
146 63 36 75 36 75
147 194 36 75 36 75
148 170 36 75 36 75
149 196 36 75 36 75
150 85 36 75 36 75
151 93 36 75 36 75
152 79 36 75 36 75
153 656 36 75 36 75
154 56 36 75 36 75
155 93 36 75 36 75
156 28 36 75 36 75
157 4 35 75 35 75
158 3 35 75 35 75
159 82 35 75 35 75
160 48 35 75 35 75
161 64 35 75 35 75
162 72 35 75 35 75
163 86 35 75 35 75
164 12 35 75 35 75
165 73 35 75 35 75
166 77 35 75 35 75
167 2162 35 75 35 75
168 854 35 75 35 75
169 51 35 75 35 75
170 61 35 75 35 75
171 11 35 75 35 75
172 8 35 75 35 75
173 16 35 75 35 75
174 58 35 75 35 75
175 50 35 75 35 75
176 53 35 75 35 75
177 8 35 75 35 75
178 48 35 75 35 75
179 235 35 75 35 75
180 38 35 75 35 75
181 75 35 75 35 75
182 25 35 75 35 75
183 12 35 75 35 75
184 18 35 75 35 75
185 51 35 75 35 75
186 19 35 75 35 75
187 22 35 75 35 75
188 1595 35 75 35 75
189 77 35 75 35 75
190 1673 35 75 35 75
191 42 35 75 35 75
192 120 35 75 35 75
193 66 35 75 35 75
194 53 35 75 35 75
195 66 35 75 35 75
196 6 35 75 35 75
197 5 35 75 35 75
198 36 35 75 35 75
199 54 35 75 35 75
200 46 35 75 35 75
class : SpatialPointsDataFrame
features : 13135
extent : 35, 37, 73, 76 (xmin, xmax, ymin, ymax)
crs : NA
variables : 3
names : Carbon, Latitude, Longitude
min values : 1, 35, 73
max values : 5829, 37, 76
R Script:
library(sp) # vector data
library(raster) # raster data
library(rgdal) # input/output, projections
library(rgeos) # geometry ops
library(spdep) # spatial dependence
foresta<-carbonstock
head(carbonstock)
data<-data.frame(carbonstock$Longitude,carbonstock$Latitude,carbonstock$Carbon)
data<-data.frame(carbonstock)
# points from scratch
coords = cbind(carbonstock$Latitude, carbonstock$Longitude)
sp = SpatialPoints(coords)
# make spatial data frame
spdf = SpatialPointsDataFrame(coords, data)
spdf = SpatialPointsDataFrame(sp, data)
# promote data frame to spatial
coordinates(data) = cbind(carbonstock$Latitude, carbonstock$Longitude)
coordinates(data) = ~lon + lat
# back to data
as.data.frame(data)
plot(data,)
library(raster)
dfr <- rasterFromXYZ(data) #Convert first two columns as lon-lat and third as value
plot(dfr)
dfr
library(raster)
# create spatial points data frame
spg <- data
x<-carbonstock$Latitude
y<-carbonstock$Longitude
coordinates(spg) <- ~ x + y
# coerce to SpatialPixelsDataFrame
gridded(spg) <- TRUE
# coerce to raster
rasterDF <- raster(spg)

Related

Extracting first n elements from a list of vectors to a new list

I have a list with multiple vectors such as
List1<- list(v1=sample(1:100,150,replace=TRUE),v2=sample(1:100,150,replace=TRUE),v3=sample(1:100,150,replace=TRUE),v4=sample(1:100,150,replace=TRUE))
The output is something like
$v1
[1] 37 20 26 46 70 70 46 58 99 45 33 51 54 71 1 46 70 39 97 50 68 15 3 78 99 85 26
[28] 41 49 64 1 49 91 52 23 67 53 96 79 64 7 21 10 56 42 36 4 32 27 23 8 3 74 58
[55] 58 78 51 59 24 88 69 31 58 95 19 27 65 34 16 66 56 7 68 85 43 44 34 18 34 25 54
[82] 84 69 9 35 53 19 100 28 31 66 64 24 73 39 28 70 47 57 17 67 27 54 79 15 19 23 60
[109] 60 84 65 98 47 57 10 38 65 48 31 23 12 94 81 16 100 45 71 61 24 6 33 100 22 84 74
[136] 92 43 49 41 59 57 40 22 35 78 46 83 27 65 20
$v2
[1] 97 45 47 23 32 88 45 93 88 64 100 21 85 70 99 70 59 54 20 45 62 63 91 42 24 74 20
[28] 29 3 67 96 31 1 11 50 24 32 14 27 41 73 89 92 55 45 83 77 34 94 51 80 93 52 2
[55] 73 51 63 56 78 58 48 91 38 97 28 53 94 60 45 24 2 74 17 38 40 49 61 81 59 29 80
[82] 60 47 9 71 37 9 10 68 98 6 34 52 24 36 41 28 37 46 75 63 10 30 69 23 100 29 83
[109] 88 60 87 53 51 94 31 48 17 71 14 23 12 71 21 47 79 23 96 42 95 2 16 81 72 64 15
[136] 28 40 40 77 13 99 87 29 95 64 18 46 49 25 74
$v3
[1] 67 22 45 78 79 92 24 25 22 95 67 91 4 72 37 49 87 32 80 2 54 96 48 79 39 47 75
[28] 3 24 37 93 64 97 83 50 85 61 93 78 53 35 77 14 12 70 74 24 54 26 73 33 28 19 35
[55] 87 26 62 85 29 62 21 65 27 64 8 67 76 86 74 80 68 66 34 97 83 11 30 69 20 79 90
[82] 36 18 7 58 9 32 88 64 22 76 99 71 62 10 62 50 70 79 37 73 100 4 47 17 96 79 29
[109] 55 58 79 46 45 81 57 74 21 69 86 60 21 3 79 87 8 88 79 65 82 72 36 97 1 73 82
[136] 29 97 81 88 42 35 10 98 66 17 76 85 84 31 51
$v4
[1] 66 15 87 78 6 2 57 57 21 46 97 83 70 56 2 92 2 57 89 69 58 9 24 89 65 48 92
[28] 87 91 100 91 99 21 16 26 83 38 90 57 14 5 74 72 33 52 59 92 85 8 80 53 82 41 91
[55] 69 33 8 14 10 25 88 35 21 48 60 81 57 13 16 69 50 30 16 1 89 73 17 3 46 86 71
[82] 75 16 91 73 25 92 93 54 59 59 27 25 51 9 27 18 74 66 5 51 60 63 17 94 25 22 100
[109] 4 95 57 89 56 26 36 52 40 74 31 46 16 18 9 27 51 87 74 50 28 87 85 52 34 76 31
[136] 56 84 25 91 33 98 80 2 30 49 90 23 21 2 26
I want to extract the first 6 elements of each of the vectors, and save it to a new list, such that the output may look like
$v1
[1] 37 20 26 46 70 70
$v2
[1] 97 45 47 23 32 88
$v3
[1] 67 22 45 78 79 92
$v4
[1] 66 15 87 78 6 2
I have tried sapply(List1,head,6), but this is not the output I was looking for.

Plot dots over dateline with ggplot in R

I'm trying to plot some dots on a map in R but only half of them show up. I suppose it might have something to do with the dateline.
My sample data:
sites
longitude latitude
1 -136 53
2 140 33
3 -176 59
4 -138 47
5 -170 61
6 142 35
7 -128 45
8 -178 55
9 -140 57
10 -168 39
11 158 39
12 -178 45
13 144 45
14 -166 25
15 162 47
16 -178 35
17 -178 43
18 146 39
19 138 25
20 -178 25
21 140 33
22 166 45
23 142 37
24 164 45
25 -162 39
26 156 39
27 -178 53
28 -178 29
29 -150 59
30 -178 35
31 -176 57
32 138 43
33 164 59
34 -152 37
35 152 39
36 -178 61
37 164 35
38 174 51
39 -176 29
40 154 41
41 -140 51
42 120 35
43 -178 53
44 -168 55
45 -172 65
46 -126 59
47 -178 53
48 -144 45
49 -178 37
50 -122 59
51 -178 27
52 -176 43
53 128 27
54 -130 33
55 134 37
56 -156 55
57 -176 41
58 148 47
59 146 37
60 -164 37
61 158 35
62 124 47
63 162 63
64 -178 47
65 144 37
66 -170 25
67 120 41
68 -164 41
69 -178 45
70 140 47
71 164 45
72 -164 39
73 -178 41
74 120 35
75 -150 41
76 -154 45
77 124 43
78 148 39
79 -162 43
80 152 35
81 -138 39
82 124 29
83 -168 51
84 146 49
85 -178 35
86 -152 49
87 -162 43
88 160 53
89 152 35
90 126 31
91 -156 47
92 -174 33
93 164 41
94 160 37
95 -154 25
96 -178 41
97 142 49
98 -178 35
99 -178 51
100 138 35
101 -120 41
102 -160 47
103 148 37
104 166 41
105 -148 37
106 146 37
107 -168 53
108 -178 37
109 132 29
110 -122 57
The code:
wrld2 = st_as_sf(map('world2', plot=F, fill=T))
ggplot() +
geom_sf(data=wrld2, fill='lightgrey') +
geom_point(data=sites, aes(x = longitude, y = latitude), size = 4,
shape = 22, fill = "darkblue") +
coord_sf(xlim=c(-180,-120), ylim=c(25,65)) +
geom_sf(data=wrld2, fill='lightgrey') +
geom_point() +
geom_point(data=sites, aes(x = longitude, y = latitude), size = 4,
shape = 21, fill = "darkred") +
coord_sf(xlim=c(120,240), ylim=c(25,65)) +
theme(panel.background = element_rect(fill = "aliceblue"))
Only the Western part of the dateline is plotted with the correct dots (others are put on top):
The problem is that the blue data points are not plotted in their specific range (-180,-120 and 25,65) because of the two coordinate systems/dateline around +/- 180°. I can't seem to figure how to define the correct xlim so that the longitude ticks remain the same on the x-axis. Has anyone an idea what is missing or needs to be re-defined? Many thanks.

reprex setting output width

How do I set the width of a reprex output?
Say I have a code like this:
(x <- 1:100)
I get this with reprex::reprex(venue = "so")
(x <- 1:100)
#> [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#> [18] 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
#> [35] 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
#> [52] 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
#> [69] 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
#> [86] 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
How can I increase the width of the output to output something like this
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
[51] 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
Possible Solutions
One option that I have found but I find rather "un-tidy" is this (include options(width = ...) at the top of the code. But I don't want it to show up in the output, I'd prefer setting the width in the reprex-call.
options(width = 205)
(x <- 1:100)
#> [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
#> [51] 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
reprex() allows for knitr's opts-chunk, but I can't get it working with reprex::reprex(venue = "so", opts_chunk = list(out.width = 205)) (which might be related to #421 as pointed out here (Long lines of text output))
Any better solutions?
reprex has a syntax for setting these options but not including them in the output markdown (see here for examples). In this case:
reprex({
#+ setup, include = FALSE
options(width=205)
#+ actual-reprex-code
(x <- 1:100)
}, venue = 'so')
outputs your desired format:
(x <- 1:100)
#> [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
#> [51] 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
Created on 2018-09-21 by the reprex package (v0.2.1)

Generate all possible dataframes by combining rows of a initial dataframe

I have this dataframe:
> prueba
S1 S2 S3 M1 M2 M3
Block-1 23 67 143 37 87 104
Block-2 43 76 145 43 76 103
Block-3 35 87 165 44 88 116
Block-4 45 55 145 43 97 156
Block-5 33 65 159 54 96 145
Block-6 37 54 139 51 91 166
Block-7 33 56 149 56 93 156
Block-8 38 55 135 57 94 167
Block-9 33 52 134 59 98 168
So, I want to generate all possible diferent dataframes by combining only 6 of the 9 rows that have the initial dataframe. Thanks to the "combn" function, I know there are 84 combinations of 6 blocks, now I want to have in a list of dataframes all those combinations with their respective S1,S2,S3,M1,M2,M3 values.
The solution would be like this:
> prueba1
S1 S2 S3 M1 M2 M3
Block-1 23 67 143 37 87 104
Block-2 43 76 145 43 76 103
Block-3 35 87 165 44 88 116
Block-4 45 55 145 43 97 156
Block-5 33 65 159 54 96 145
Block-6 37 54 139 51 91 166
> prueba2
S1 S2 S3 M1 M2 M3
Block-1 23 67 143 37 87 104
Block-2 43 76 145 43 76 103
Block-3 35 87 165 44 88 116
Block-4 45 55 145 43 97 156
Block-5 33 65 159 54 96 145
Block-7 33 56 149 56 93 156
·
·
·
Thanks.

How can we determine a transition point in a curve for a bar graph

I have plotted a bar graph and knowing that the transition in the curve takes place around values 21 to 25 , I want to find such point in general.
barplot(Views$V2,names=Views$V1,las=2,cex.names=0.2,border="blue",xpd=FALSE)
abline(v=25,col="red")
Any help is appreciated
Update :
A part of my data frame is given below. It is sorted by values V2 . By transition what I want to say is that a point in the sorted data where there is a large deviation and after which it continues in that pattern. So a point where there is difference in patterns.
V1 V2
1 1 16154424
2 2 3701944
3 3 1618377
4 4 903302
5 5 569824
6 6 389772
7 7 281751
8 8 212450
9 9 166364
10 10 133339
11 11 109410
12 12 90934
13 13 77155
14 14 66124
15 15 57861
16 16 50765
17 17 44805
18 18 39996
19 19 35850
20 20 32492
21 21 29522
22 22 27152
23 23 24821
24 24 22619
25 25 21238
26 26 19639
27 27 18320
28 28 16867
29 29 15890
30 30 14936
31 31 14252
32 32 13150
33 33 12696
34 34 11656
35 35 11191
36 36 10951
37 37 10232
38 38 9605
39 39 9058
40 40 8916
41 41 8531
42 42 8010
43 43 7932
44 44 7436
45 45 6991
46 46 6750
47 47 6613
48 48 6254
49 49 6292
50 50 5731
51 51 5659
52 52 5551
53 53 5396
54 54 5122
55 55 4845
56 56 4860
57 57 4591
58 58 4504
59 59 4233
60 60 4371
61 61 4014
62 62 4083
63 63 3923
64 64 3796
65 65 3616
66 66 3519
67 67 3466
68 68 3409
69 69 3357
70 70 3215
71 71 3118
72 72 3081
73 73 3040
74 74 2951
75 75 2808
76 76 2797
77 77 2829
78 78 2714
79 79 2564
80 80 2563
81 81 2445
82 82 2528
83 83 2443
84 84 2316
85 85 2314
86 86 2212
87 87 2215
88 88 2102
89 89 2172
90 90 2020
91 91 2108
92 92 2020
93 93 2027
94 94 1982
95 95 1936
96 96 1836
97 97 1801
98 98 1850
99 99 1751
100 100 1810
It has to be something to do with the ratios of differences maybe and something which can be evident after observing the graph.

Resources