Combine data split by factor using "by' function back into one - r

I have a data frame final which looks like this
TrackIndex Time x_position y_position
1 1 0.1034 425 171
2 1 0.1379 425 169
3 1 0.1724 427 166
.........
125 25 1.1030 462 397
126 25 1.1380 462 397
127 25 1.1720 462 397
128 25 1.2070 462 397
129 25 1.2410 461 398
130 25 1.2760 462 399
131 25 1.3100 461 399
132 25 1.3450 461 399
133 25 1.3790 460 399
134 25 1.4140 460 399
.....
268 41 1.8280 302 280
269 41 1.8620 303 279
270 41 1.8970 302 280
271 41 1.9310 302 280
272 41 1.9660 302 281
273 41 2.0000 302 281
274 41 2.0340 302 281
275 41 2.0690 302 282
276 41 2.1030 302 282
277 41 2.1380 302 282
278 41 2.1720 302 283
........
I sorted this data frame by factor "TrackIndex", and selected only the first 5 rows for each unique TrackIndex using "by" function.
finalbyfactor<-by(data = final, INDICES = final$TrackIndex, FUN = function(x) head(x, 5))
This gave me the following results
> finalbyfactor
final$TrackIndex: 1
TrackIndex Time x_position y_position
1 1 0.1034 425 171
2 1 0.1379 425 169
3 1 0.1724 427 166
4 1 0.2069 427 167
5 1 0.2414 427 167
-----------------------------------------------------------------------------
final$TrackIndex: 25
TrackIndex Time x_position y_position
125 25 1.103 462 397
126 25 1.138 462 397
127 25 1.172 462 397
128 25 1.207 462 397
129 25 1.241 461 398
-----------------------------------------------------------------------------
final$TrackIndex: 41
TrackIndex Time x_position y_position
268 41 1.828 302 280
269 41 1.862 303 279
270 41 1.897 302 280
271 41 1.931 302 280
272 41 1.966 302 281
Now I want to recombine the selected rows into one data frame as the initial one. How can I do it?
I tried rbind and merge, both did not work.

Related

Change the start date of function `lubridate::yday`

The function ludridate::yday returns the day of the year as an integer:
> lubridate::yday("2020-07-01")
[1] 183
I would like to be able to calculate the day of the year assuming a different yearly start date. For example, I would like to start all years on July 1st (07-01), such that I could call:
> lubridate::yday("2020-07-01", start = "2020-07-01")
[1] 1
I could call :
> lubridate::yday("2020-07-01") - lubridate::yday("2020-06-30")
[1] 1
But not only this would fail to account for leap years, it would be difficult to account for a date with a 2021 year (or any date that crosses the January 1st threshold for any given year):
> lubridate::yday("2021-01-01") - lubridate::yday("2020-06-30")
[1] -181
After working a little bit with this on my own, this is what I have created:
valiDATE <- function(date) {
stopifnot(`date must take the form of "MM-DD"` = stringr::str_detect(date, "^\\d{2}-\\d{2}$"))
}
days <- function(x, end = "06-30") {
valiDATE(end)
calcdiff <- function(x) {
endx <- glue::glue("{lubridate::year(x)}-{end}")
if(lubridate::yday(x) > lubridate::yday(endx)) {
diff <- ceiling(difftime(x, endx, units = "days"))
} else {
endx <- glue::glue("{lubridate::year(x)-1}-{end}")
diff <- ceiling(difftime(x, endx, units = "days"))
}
unclass(diff)
}
purrr::map_dbl(x, calcdiff)
}
day_vec <- seq(as.Date("2020-07-01"), as.Date("2021-06-30"), by = "days")
days(day_vec)
[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] 38 39 40 41 42 43 44 45 46 47 48 49 50 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] 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 101 102 103 104 105 106 107 108 109 110 111
[112] 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
[149] 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
[186] 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
[223] 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
[260] 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
[297] 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
[334] 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
I would still like to see other solutions. Thanks!
Adding or subtracting months from your date to the desired start of the year can be helpful in this case.
For your example vector of dates day_vec, you can subtract six months from all the dates if you want to start your year on July 1.
day_vec <- seq(as.Date("2020-07-01"), as.Date("2021-06-30"), by = "days")
day_vec2 <- day_vec %m-% months(6) #Substracting because the new year will start 6 months later
yday(day_vec2) #Result is similar to what you desired.
The point to keep in mind is whether your new beginning of the year is before or after the conventional beginning. If your year starts early, you should add months and vice-versa.

Using R to create a 'change in x' column for multiple x columns

Im currently trying to calculate change in amount of SWE for multiple sites (50) so as to attain snow melt rates (change per day). I think I have the right formula but trying to loop it doesn't seem to be working, my code and error below.
site <- colnames(winter_pr_swe[,55:104])
for (i in site){
sitex <- paste0(i)
ratex <- paste0('rate',i)
winter_pr_swe1 <- winter_pr_swe %>% mutate(ratex = (sitex - lag(sitex))
}
Error: unexpected '}' in: "winter_pr_swe1 <- winter_pr_swe %>% %>% mutate(ratex = (sitex - lag(sitex))
}"
Here is the head of the data set for reference, each column being the SWE levels for a single site, each row being a days measurement:
head(winter_pr_swe[,55:104])
X303 X308 X310 X316 X327 X337 X340 X378 X386 X409 X416 X426
1 157 71 234 287 361 163 86 292 241 348 109 183
2 157 150 272 333 384 165 102 300 302 409 130 185
3 157 150 274 340 396 168 107 310 312 419 130 190
4 157 150 274 343 406 168 157 315 312 419 122 190
5 157 147 274 351 409 168 193 318 318 434 107 190
6 165 140 274 351 427 168 193 335 318 434 107 234
X431 X465 X486 X488 X491 X511 X519 X527 X532 X538 X580 X586
1 592 178 69 53 292 427 64 137 450 175 312 246
2 658 208 97 122 340 490 155 137 508 190 340 282
3 668 218 97 124 351 490 157 137 513 203 343 300
4 676 218 94 124 351 490 157 152 521 203 343 302
5 696 224 86 119 351 490 152 160 531 213 353 323
6 726 226 79 109 351 485 145 168 549 218 368 333
X589 X595 X617 X622 X624 X629 X632 X640 X665 X705 X715 X737
1 310 41 351 300 315 206 358 66 46 358 157 478
2 338 43 406 320 330 231 391 155 48 483 170 511
3 353 33 406 328 338 241 396 155 48 485 180 521
4 356 30 406 330 338 241 399 155 48 485 183 523
5 363 25 401 333 353 267 427 155 56 485 183 556
6 368 15 401 345 376 267 460 145 56 485 198 599
X739 X755 X757 X762 X780 X827 X839 X840 X843 X857 X861 X866
1 168 201 274 254 462 356 74 526 183 180 58 112
2 196 244 310 257 533 363 74 584 224 196 145 160
3 201 249 315 267 549 376 74 589 226 196 145 175
4 206 251 318 267 551 376 74 592 229 196 145 160
5 226 249 320 282 587 376 91 620 239 201 142 155
6 226 249 320 282 599 417 91 648 241 201 119 145
X874 X877
1 615 23
2 671 152
3 678 155
4 681 155
5 709 140
6 752 140

Length of list object returns 1 in R when not 1

I have a list x
X1
1 0.8
2 1.0
3 661.7
4 661.8
5 661.9
6 662.3
7 662.6
8 662.7
9 663.3
10 663.6
11 663.7
12 663.9
13 664.0
14 664.1
15 664.3
16 664.4
17 664.5
18 664.7
19 665.1
20 666.9
21 667.5
22 668.2
23 668.3
24 669.7
25 670.3
26 670.8
27 671.1
28 672.0
29 672.1
30 674.8
31 675.3
32 677.5
33 677.9
34 678.5
35 678.9
36 679.0
37 686.6
38 687.6
39 714.1
40 899.1
41 900.4
42 901.1
43 901.3
44 902.7
45 908.3
46 908.7
47 908.9
48 909.0
49 909.2
50 910.0
51 910.1
52 910.3
53 910.6
54 910.7
55 911.3
56 911.4
57 911.6
58 911.8
59 912.6
60 912.7
61 912.8
62 913.0
63 913.1
64 913.2
65 913.3
66 913.7
67 913.9
68 914.0
69 914.2
70 914.3
71 914.4
72 914.6
73 915.2
74 915.3
75 915.5
76 915.6
77 915.7
78 915.9
79 916.0
80 916.1
81 916.3
82 916.5
83 916.6
84 916.7
85 916.9
86 917.3
87 917.5
88 917.6
89 917.8
90 917.9
91 918.0
92 918.2
93 918.3
94 918.5
95 918.6
96 918.8
97 918.9
98 919.0
99 919.2
100 919.3
101 919.5
102 919.6
103 919.7
104 919.9
105 920.0
106 920.2
107 920.3
108 920.5
109 920.6
110 920.8
111 920.9
112 921.0
113 921.1
114 921.2
115 921.3
116 921.3
117 921.5
118 921.6
119 921.7
120 921.8
121 922.0
122 922.1
123 922.4
124 922.5
125 922.6
126 922.7
127 922.9
128 923.0
129 923.2
130 923.3
131 923.5
132 923.6
133 923.8
134 923.9
135 927.2
136 927.3
137 927.4
138 927.6
139 927.7
140 927.8
141 928.0
142 928.1
143 928.3
144 928.4
145 928.5
146 928.7
147 928.8
148 928.9
149 929.1
150 929.2
151 929.3
152 929.5
153 929.6
154 929.8
155 929.9
156 930.1
157 930.2
158 930.3
159 930.3
160 930.5
161 930.6
162 930.7
163 930.9
164 931.0
165 931.1
166 931.2
167 931.3
168 931.4
169 931.5
170 931.7
171 931.8
172 932.0
173 932.0
174 932.1
175 932.2
176 932.4
177 932.5
178 932.6
179 932.7
180 933.3
181 933.4
182 933.6
183 933.7
184 933.8
185 934.5
186 934.7
187 934.8
188 934.9
189 935.0
190 935.2
191 935.3
192 935.3
193 935.5
194 935.6
195 935.7
196 935.8
197 936.0
198 936.1
199 936.3
200 936.4
201 936.5
202 936.7
203 936.8
204 936.9
205 937.1
206 937.2
207 937.4
208 937.5
209 937.7
210 937.8
211 937.9
212 938.1
213 938.2
214 938.4
215 938.5
216 938.7
217 938.9
218 939.0
219 939.2
220 939.4
221 939.7
222 939.9
223 940.3
224 940.7
225 940.9
226 941.4
227 941.7
228 942.1
229 942.6
230 942.7
231 943.3
232 943.5
233 943.9
234 944.9
235 945.0
236 945.1
237 945.4
238 945.6
239 945.8
240 945.9
241 946.2
242 947.6
243 947.9
244 948.2
245 948.3
246 948.5
247 948.6
248 948.8
249 948.9
250 949.5
251 949.6
252 951.8
253 951.9
254 952.0
255 952.1
256 952.5
257 952.6
258 953.0
259 953.3
260 953.4
261 953.5
262 953.7
263 953.8
264 953.9
265 954.1
266 954.2
267 954.4
268 954.5
269 954.7
270 954.8
271 955.0
272 955.1
273 955.2
274 955.4
275 955.5
276 955.6
277 955.7
278 955.9
279 956.0
280 956.1
281 956.3
282 956.4
283 956.5
284 956.6
285 956.9
286 957.2
287 957.3
288 957.4
289 957.5
290 957.9
291 958.9
292 959.0
293 959.3
294 959.5
295 959.9
296 960.0
297 960.2
298 960.5
299 960.6
300 960.8
301 960.8
302 961.4
303 961.5
304 961.6
305 961.7
306 961.8
307 961.9
308 968.8
309 969.1
310 970.0
311 970.5
312 970.7
313 974.2
314 998.7
315 998.8
316 998.9
317 999.1
318 999.2
319 1000.3
320 1001.2
321 1001.4
322 1001.5
323 1001.6
324 1001.7
325 1003.2
326 1003.4
327 1003.6
328 1004.2
329 1004.3
330 1004.4
331 1004.5
332 1004.6
333 1005.3
334 1005.4
335 1005.5
336 1005.6
337 1005.7
338 1005.9
339 1006.0
340 1006.1
341 1006.8
342 1006.9
343 1007.1
344 1007.2
345 1007.3
346 1007.4
347 1007.6
348 1007.7
349 1007.8
350 1008.0
351 1008.1
352 1008.7
353 1008.8
354 1008.9
355 1009.0
356 1009.2
357 1009.3
358 1009.3
359 1009.5
360 1009.6
361 1009.7
362 1009.8
363 1010.0
364 1010.2
365 1010.4
366 1010.5
367 1010.6
368 1010.7
369 1010.9
370 1011.0
371 1011.1
372 1011.2
373 1011.4
374 1011.5
375 1011.6
376 1011.7
377 1011.9
378 1012.0
379 1012.1
380 1012.2
381 1012.3
382 1012.4
383 1012.6
384 1012.7
385 1012.8
386 1013.0
387 1013.2
388 1013.4
389 1013.5
390 1013.6
391 1013.6
392 1013.8
393 1013.9
394 1014.0
395 1014.0
396 1014.3
397 1014.7
398 1014.8
399 1014.9
400 1015.7
401 1015.8
402 1016.0
403 1016.1
404 1016.2
405 1016.5
406 1016.6
407 1016.9
408 1017.0
409 1017.1
410 1017.3
411 1017.4
412 1017.5
413 1017.7
414 1017.8
415 1017.8
416 1018.3
417 1018.5
418 1026.6
419 1027.0
420 1027.3
421 1027.4
422 1027.7
423 1028.6
424 1029.1
425 1029.9
426 1030.0
427 1030.2
428 1270.0
429 1270.1
430 1270.2
431 1270.3
432 1270.4
433 1270.5
434 1270.7
435 1270.7
436 1270.9
437 1271.0
438 1271.3
439 1271.4
440 1272.3
441 1272.5
442 1273.1
443 1273.2
444 1273.3
445 1273.4
446 1273.5
447 1273.8
448 1274.0
449 1274.1
450 1274.3
451 1274.4
452 1274.6
453 1274.7
454 1274.8
455 1274.9
456 1275.1
457 1275.3
458 1275.5
459 1275.6
460 1275.8
461 1275.9
462 1276.1
463 1276.2
464 1276.3
465 1276.4
466 1276.6
467 1276.7
468 1276.8
469 1277.2
470 1277.3
471 1277.5
472 1277.6
473 1277.7
474 1277.9
475 1278.0
476 1278.1
477 1278.2
478 1278.3
479 1278.4
480 1278.5
481 1278.7
482 1279.0
483 1279.0
484 1279.1
485 1279.3
486 1279.3
487 1279.5
488 1279.6
489 1279.7
490 1279.8
491 1280.3
492 1280.4
493 1280.7
494 1280.8
495 1280.9
496 1281.1
497 1281.3
498 1281.4
499 1281.5
500 1282.3
501 1283.0
502 1283.1
503 1284.0
504 1284.8
505 1284.9
506 1285.0
507 1285.1
508 1285.4
which has a length of 508, although when I use length(x) it returns 1. I have tried to the function
length(as.vector(x))
although this also does not work and returns 1. Is there another form that I should convert this list to so that I can accurately find the length? For reference, I am using the length to duplicate other elements using the rep_len() function.
as.vector on a data.frame returns the data.frame itself as there is no method for as.vector with data.frame
methods('as.vector')
#[1] as.vector,abIndex-method as.vector,ANY-method as.vector,dgCMatrix-method as.vector,dgeMatrix-method
#[5] as.vector,diagonalMatrix-method as.vector,dsCMatrix-method as.vector,ldenseMatrix-method as.vector,Matrix-method
#[9] as.vector,ndenseMatrix-method as.vector,sparseVector-method as.vector.factor as.vector.Matrix*
#[13] as.vector.sparseVector*
We can also check the reverse i.e. on data.frame
grep('^as\\.', methods(class = 'data.frame'), value = TRUE)
#[1] "as.data.frame.data.frame" "as.data.table.data.frame"
#[3] "as.list.data.frame" "as.matrix.data.frame" "as.tbl.data.frame"
and the length is the same as the number of columns of data.frame i.e. here it is 1. Instead, we need nrow(x)
as.vector(mtcars) # nothing changed
length(as.vector(mtcars))
#[1] 11
But, suppose, if we do
nrow(mtcars)
#[1] 32
length can also be applied on the vector by extracting the column with $ or [[
length(mtcars[[1]])

inconsistency in a for loop with lists

I have downloaded the historical stock prices of a list of 218 stocks. I want check whether it is populated with the the most recent date or not. I have written a function to that effect, by name check.date
function(snlq){
j <- 1;
for(i in 1:length(snlq)){
ind <- index(snlq[[i]])
if(identical(ind[length(ind)],"2018-05-04") == FALSE){
s[j] <- i
j <- j+1
}
}
return(s);
}
snlq is list of stocks with length 218 and of class list
But when I run it, I get the following output:
check.date(snlq)
[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] 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
[65] 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] 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
[129] 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
[161] 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
[193] 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 356 358 359 360 361 362
[225] 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
[257] 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
[289] 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458
[321] 459 460 461 462 463 464 465 466 467 468 469 470
How can the output be of length more than 218? Also I have checked that snlq[[1]] is up to date; then why is 1 in the output?
This might seem like a simple for loop problem, but is perplexing me.
Very many thanks for your time and effort...
It seems the problem is that s is not created in scope in which it is updated and used. #Dave2e has correctly pointed out in above comment. The most logical error seems to me is that s has been created in global space that's why your function is not giving error, otherwise your function would have not run.
There are many ways to fix the problem. One of the option can be as:
check.date <- function(snlq){
j <- 1;
ss <- integer() #declare before use in function scope
for(i in 1:length(snlq)){
ind <- index(snlq[[i]])
if(identical(ind[length(ind)],"2018-05-04") == FALSE){
s = c(s,j) #Kind of adding an element to vector s
j <- j+1
}
}
return(s);
}
I cannot check this result without a reproducible example, but I think this will simplify your function greatly.
check.data <- function(input, today) {
result <- sapply(input, function(x) {
ind <- index(x)
!identical(ind[length(ind)], today)
})
which(result)
}

Create SpatialLinesDataFrame from point coordinates

I am doing a animal tracking project. My data "finaltrimmed" looks like this
TrackIndex Time x_position y_position
1 1 0.1034 425 171
2 1 0.1379 425 169
3 1 0.1724 427 166
.........
125 25 1.1030 462 397
126 25 1.1380 462 397
127 25 1.1720 462 397
128 25 1.2070 462 397
129 25 1.2410 461 398
130 25 1.2760 462 399
131 25 1.3100 461 399
132 25 1.3450 461 399
133 25 1.3790 460 399
134 25 1.4140 460 399
.....
268 41 1.8280 302 280
269 41 1.8620 303 279
270 41 1.8970 302 280
271 41 1.9310 302 280
272 41 1.9660 302 281
273 41 2.0000 302 281
274 41 2.0340 302 281
275 41 2.0690 302 282
276 41 2.1030 302 282
277 41 2.1380 302 282
278 41 2.1720 302 283
........
I wish to create a line for each unique TrackIndex, which basically tracks how each individual insect move over time. And from there I want create a SpatialLinesDataFrame based on TrackIndex. Eventually, I want to use “buffer”function in “adehabitatMA” package to create a buffer area around each line.
I was able to create a SpatialPointsDataFrame using the following command.
xy<-cbind(finaltrimmed$x_position,finaltrimmed$y_position)
MatrixofPoints<-matrix(xy,ncol=2)
points<-SpatialPoints(MatrixofPoints)
dataframe=data.frame(finaltrimmed$TrackIndex)
df.points<-SpatialPointsDataFrame(points,dataframe)
However, I was not able to create a SpatialLinesDataFrame in a similar way.
My idea is to split the data frame “final trimmed” first with “split” function.
splitfinal<-split(finaltrimmed,finaltrimmed$TrackIndex)
which gives me the following data structure
$1
TrackIndex Time x_position y_position newindex
1: 1246 347.0 316 214 1
2: 1246 347.0 316 214 2
......
57: 1246 348.9 325 201 57
58: 1246 349.0 330 201 58
TrackIndex Time x_position y_position newindex
$25
TrackIndex Time x_position y_position newindex
1: 1318 363.6 375 422 1
2: 1318 363.7 375 422 2
.....
57: 1318 365.6 399 406 57
58: 1318 365.6 400 406 58
From there, I can cbind the x and y positions in “splitfinal” (this step didn’t work out because “splitfinal” is a list of lists). I am also not sure how to create a Lines-class, which is required to create a SpatialLinesDataFrame.
I am been stuck for many days and could not figure a way.
Can anyone help?
Here is an approach that should work:
Example data:
finaltrimmed <- read.table(text="TrackIndex Time x_position y_position
1 1 0.1034 425 171
2 1 0.1379 425 169
3 1 0.1724 427 166
130 25 1.2760 462 399
131 25 1.3100 461 399
132 25 1.3450 461 399
133 25 1.3790 460 399
134 25 1.4140 460 399
274 41 2.0340 302 281
275 41 2.0690 302 282
276 41 2.1030 302 282
277 41 2.1380 302 282
278 41 2.1720 302 283")
Solution:
library(raster)
ft <- split(finaltrimmed, finaltrimmed$TrackIndex)
z <- lapply(ft, function(i) spLines(as.matrix(i[, c('x_position', 'y_position')]), attr=data.frame(TrackIndex=i$TrackIndex[1])))
names(z) <- NULL
zz <- do.call(bind, z)

Resources