I am an R novice and have been running into a brick wall with what should be a simple for loop process. Data consists of a list with dimensions of 81161 by 9. The observations are of individuals over time. The current need is to isolate an unique group of observations and randomly extract one of the observation data points. At this stage I have reviewed and been attempting a few options none of which are being executed property. First the for loop then apply.
To provide a better idea of the workflow I have outlined. This should be a relatively straight forward split-apply-combine. The apply is a sample with restriction to unique individual_days. To do this the code goes through the basic defining of over all dimensions, then defining of unique values, a sort and rank (from which the unique individuals_day are set to an ordianl scale and then these are linked back to the originsl data using the individuals_day as the key). From this point I have attempted two alternatives with the for loops --- first using a split by rank to provide DSrank$'1, 2, 3...n' (attempted to be used in example 2) and using the subset seen in example 1. A single sample would then be extracted at random and collated into a sub-dataset. From this point other analysis will be performed.
### example 1: for loop
SDS <- list()
for(i in 1:length(UID)) {
`SDS[[i]] <- sample(nrow(SplitDS$[i]), 1, replace=FALSE)
`SDS[[i]]["Samples"] < i
}
head(SDS)
### example 2: for loop
SDS <- list()
for(i in 1:length(UID)) {
`SubSDID <- subset(DSID, DSrank == 'i', )
`SDS <- sample(nrow(SubSDID), 1, replace=FALSE)
}
head(SDS)
### example 3: apply subset
bootstrap <- lapply(1:length(UID), function(i) {
`samp <- sample(1:nrow(DSID$DSrank, rep = TRUE)
`DSID$DSrank[sampl, ]
}
These have been based off examples I have found through CRAN, stackoverflow, and other R-code search results.
If you have any suggestions, tips, or tricks that you could share it would be greatly appreciated.
MB