lundi 14 septembre 2015

R: Random sampling a minimum number of observations from a category

I have location data from GPS collars and I am trying to simulate different scenarios based on the functionality of the collars in R. One of those simulations is that the collars tend to miss taking GPS points throughout the day (for various reasons). My data consists of 14 GPS points per day, and I want to randomly select (without replacement) a minimum number of 5 points, the possibility of a max 14.

In another simulation, I extracted 5 random points per day using this script from another thread here (R: Random sampling an even number of observations from a range of categories), but I do not fully understand all the different bits of the script that would allow me to alter it to get it to extract AT LEAST 5 points. Any advice most appreciated.

dat2 <- data.table(dat.r)
dat2.ss <- dat2[ , .SD[sample(1:.N,min(5,.N))], by=DayNo]

Output from data-frame (dat.r)

dput(head(dat.r, 20))
structure(list(Latitude = c(5.4118432, 5.4118815, 5.4115713, 
5.4111541, 5.4087853, 5.4083702, 5.4082527, 5.4078161, 5.4075528, 
5.407321, 5.4070598, 5.4064237, 5.4070621, 5.4070251, 5.4070555, 
5.4065127, 5.4065134, 5.4064872, 5.4056724, 5.4038751), Longitude = c(118.0225467, 
118.0222841, 118.0211875, 118.0208637, 118.0205413, 118.0206064, 
118.0204101, 118.0209272, 118.0213827, 118.0214189, 118.0217748, 
118.0223343, 118.0227079, 118.0226511, 118.0226916, 118.0220733, 
118.02218, 118.0221843, 118.0223316, 118.0198153), DayNo = c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L)), .Names = c("Latitude", "Longitude", "DayNo"), row.names = c(NA, 
20L), class = "data.frame")




Aucun commentaire:

Enregistrer un commentaire