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)
Related
I am calling table in a loop, so the variable name have to be in a variable itself. Reproducible code below:
library(FactoMineR)
data(hobbies)
htable <- matrix(as.numeric(NA), nrow=18, ncol=7,
dimnames=list( colnames(hobbies)[1:18],
names(with(hobbies,
table(Profession)))))
### Then filling the tables with values:
for(hob in rownames(htable)) {
htable[hob, ] <- with(hobbies, table(hob,
Profession))[2, ]
}
+ + > Error in table(hob, Profession) : all arguments must
have the same length
Somehow, the length of hob is taken as 1, so it is not interpreted in the context of the data frame hobbies? What is wrong?
We can use [ instead of with
for(hob in rownames(htable))
htable[hob, ] <- table(hobbies[, hob],
hobbies[, "Profession"])[2,]
-output
> htable
Unskilled worker Manual labourer Technician Foreman Management Employee Other
Reading 361 603 241 570 907 1804 154
Listening music 488 715 285 554 853 1879 150
Cinema 181 307 169 365 640 1016 92
Show 84 189 132 264 533 734 68
Exhibition 92 208 138 326 604 717 74
Computer 147 307 203 351 606 915 80
Sport 163 296 187 361 595 867 75
Walking 320 516 209 443 632 1295 112
Travelling 165 314 189 398 720 932 89
Playing music 69 105 78 152 288 430 52
Collecting 69 129 51 85 120 287 16
Volunteering 54 114 69 168 263 377 40
Mechanic 268 629 249 381 553 867 90
Gardening 266 501 162 347 501 968 92
Knitting 147 98 31 111 116 634 43
Cooking 314 435 156 313 465 1302 91
Fishing 110 223 66 94 103 168 19
TV 78 138 56 127 206 331 34
The values passed in for each loop is a string i.e. 'hob' signifies each value of the rownames,
> with(hobbies, "Reading")
[1] "Reading"
It doesn't return the value of the column 'Reading' when we wrap with hobbies although, directly we can do this
> head(with(hobbies, Reading))
[1] 1 1 1 1 1 0
Levels: 0 1
or with [ or [[
> head(hobbies[, "Reading"])
[1] 1 1 1 1 1 0
Levels: 0 1
> head(hobbies[["Reading"]])
[1] 1 1 1 1 1 0
Levels: 0 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]])
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)
}
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.
I am trying to calculate a GEE-model in the R package "geepack". The response variable is proportional, coded as (Successes, Failures). The explanatory variables are Weight(cont), Rank(cont), ColonySize(cont) and Sex(factor). The data set contains temporal non-independence of observations because over a study period of 413 days repeated behavioral measurements of the same individuals where taken. This non-independence is reflected in a column specifying the AnimalID and the day of observation (Ndate). The data set is not very large and contains 1062 observations on 165 different individuals. The complete study period is 413 days (i.e. Ndate range:1-413).
gee1<-geeglm(wl~WeightScaled+Rank+ColonySize+Sex,
data=allsub, family=binomial, id=AnimalID,
corstr="ar1")
The above model is calculated without difficulties and without noticeable delay. However, the observations are not regularly distributed over the study period (see the complete vector for Ndate below) which means the model output is not meaningful. When I include the waves argument in the model to correctly account for temporal auto-correlation R seems to get stuck or takes very long to calculate this model which should really not take so much time. What happens is that R-Gui displays "(not responding)" for more than 1 hour and the small circle (Win7) indicates that R is busy. The CPU-usage according to the task manager is mostly between 25-30%, sometimes up to 50%. So my question is: Did I make a mistake when specifying the "waves" function which cause R to hang itself or is it normal for this process to be computational very intense? (see an extract of the variable Ndate below)
Model including the waves argument:
gee1<-geeglm(wl~Weight+Rank+ColonySize+Sex,
data=allsub, family=binomial, id=AnimalID,
corstr="ar1", waves=Ndate)
The second question is more fundamental with regards to this GEE and its autocorrelation structure: Is the model able to deal with this kind of temporal autocorrelation where repeated observations of one individual are typically 5-15 but time in between varies largely (sometimes only a few days, but sometimes up to 100 days or more). Textbook examples all look very different but as I see it the principle should be the same.
Thanks very much.
> allsub$Ndate
[1] 169 169 169 43 43 5 5 5 267 267 267 267 162 162 162 162 162 256
[19] 256 256 256 256 256 263 263 263 263 263 263 176 176 176 176 176 176 183
[37] 183 183 183 183 183 190 190 190 190 190 190 190 196 196 196 196 196 196
[55] 196 284 284 284 284 291 291 291 291 175 175 175 175 175 175 175 175 199
[73] 199 199 199 199 199 199 186 186 186 186 186 186 189 189 189 189 189 189
[91] 266 266 266 266 266 266 196 196 196 196 196 196 196 242 242 242 242 242
[109] 242 207 207 207 207 207 210 210 210 210 210 245 245 245 245 245 245 302
[127] 302 302 302 302 302 302 302 217 217 217 217 217 217 217 270 270 270 272
[145] 272 272 291 291 291 220 220 220 220 220 220 220 238 238 238 238 238 238
[757] 291 291 291 291 291 291 220 220 238 238 241 241 294 294 294 294 294 294
[775] 303 303 303 263 263 263 263 263 263 263 263 263 263 316 316 309 304 304
[793] 304 323 323 19 50 99 67 67 67 22 22 22 43 60 110 178 178 178
[811] 33 115 115 115 115 96 116 116 116 116 116 116 116 116 116 116 116 26
[829] 26 122 122 122 122 122 122 122 122 122 64 40 40 40 40 40 40 40
[847] 40 40 58 58 58 58 58 58 58 58 58 58 71 71 75 85 127 78
[865] 78 12 12 12 12 12 12 12 12 12 12 15 152 152 152 152 337 337
[883] 337 337 337 337 344 344 344 344 344 344 344 82 82 82 82 82 82 82
[901] 82 82 348 348 348 348 348 348 348 348 348 351 351 351 359 359 355 355
[919] 355 354 354 345 345 345 358 358 358 358 362 362 362 331 331 349 349 361
[937] 361 378 364 364 364 369 369 369 375 375 375 373 373 373 373 342 365 365
[955] 365 365 365 365 365 365 379 379 379 379 379 379 379 379 379 379 379 379
[973] 379 379 352 352 341 382 382 382 385 373 373 373 373 373 373 368 368 368
[991] 389 389 389 389 285 285 285 308 308 309 309 321 322 326 329 329 329 329
[1009] 330 330 330 330 385 385 385 385 385 385 385 380 380 380 380 380 380 380
[1027] 386 386 386 386 390 390 390 390 365 365 393 393 393 393 393 393 393 393
[1045] 393 393 393 393 393 393 399 397 397 397 392 392 392 392 407 407 400 400
[1063] 413 413
I founds out why R crashes when including the waves argument. GEEglm does not accept two observations on the same individual conducted on the same day. This makes sense when thinking through what the model does. Hope this may help someone else.