Question

My problem sits inside a loop, I have a large dataset (DF), a subset of which looks like this:

ID     Site Species
101     4   x
101     4   y
101     4   z
102     6   x
102     6   z
102     6   a
102     6   b
103     6   a
103     6   z
103     6   c
103     6   x
103     6   y
105     6   x
105     6   y
105     6   a
105     6   z
108     1   x
108     1   a
108     1   c
108     1   z

I would like to randomly select, using each iteration of my loop (so, i) all rows of an individual ID from each Site. But crucially, only one ID from each Site. I have a separate function that subsets my large dataset for the number of Sites, so if i=1 then only one of the above Sites (for example) would be present in the subset.

If i=3, as for this posted example, then I would want all rows of 101, and either all rows of 102, 103 or 105, and all of 108.

I think something like ddply() with sample() should do it but I cannot get it to happen randomly.

Any suggestions would be greatly appreciated. thanks

James

Was it helpful?

Solution 2

How about this? I've added a function to simulate what I think your data looks like.

#dependencies
require(plyr)

#function to make data (just to work with)
make_data<-function(id){
  set.seed(id)
  num_sites<-round(runif(1)*3,0)+1
  num_sp<-round(runif(1)*7,0)+1
  sites<-sample(1:10,num_sites,FALSE)
  ldply(sites,function(x)data.frame(sites=x,sp=sample(letters[1:26],num_sp,FALSE)))
}

#make a data frame for example use (as per question)
ids<-100:200
df<-ldply(ids,function(x)data.frame(id=x,make_data(x)))

################################################
# HERE'S THE CODE FOR THE ANSWER               #
# use ddply to summarise by site & sampled ids #
filter<-ddply(df,.(sites),summarise,set=sample(id,1))
# then apply this filter to the original list
ddply(filter,.(sites),.fun=function(x){return(df[df$site==x$sites & df$id==x$set,])})

OTHER TIPS

I think you may use unique to find all possible IDs / sites and then sample from unique and subset.

For instance, let's create a dataset

# Set the RNG seed for reproducibility
set.seed(12345)
ID <- rep(100:110, c(2, 6, 3, 1, 3, 8, 9, 2, 4, 5, 6))
site <- rep(1:6, c(8, 7, 8, 11, 4, 11))
species <- sample(letters[1:5], length(ID), replace=T)

df <- data.frame(ID=ID, Site=site, Species=species)

So, df looks like:

> head(df, 15)
    ID Site Species
1  100    1       d
2  100    1       e
3  101    1       d
4  101    1       e
5  101    1       c
6  101    1       a
7  101    1       b
8  101    1       c
9  102    2       d
10 102    2       e
11 102    2       a
12 103    2       a
13 104    2       d
14 104    2       a
15 104    2       b

Summarizing the data, we have:

Site 1 -> 100, 101
Site 2 -> 102, 103, 104
Site 3 -> 105
Site 4 -> 106, 107
Site 5 -> 108
Site 6 -> 109, 110

Now, let's say I want to select out of 3 sites

# The number of sites we want to sample
num.sites <- 3
# Find all the sites
all.sites <- unique(df$Site)
# Pick the sites. 
# You may also want to check that num.sites <= length(all.sites)
sites <- sample(all.sites, num.sites)

In this case we picked

> sites
[1] 4 5 6

Ok, so now we find the IDs available for each site

# Now find the IDs in each of those sites
# simplify=F is VERY important to ensure we get a list even if every
# site has the same number of IDs
IDs <- sapply(chosen.sites, function(s)
    {
    unique(df$ID[df$Site==s])
    }, simplify=FALSE)

Which gives us

> IDs
[[1]]
[1] 106 107

[[2]]
[1] 108

[[3]]
[1] 109 110

Now pick one ID per site

# NOTE: this assumes the same ID is not found in multiple sites
# but it's easy to deal with the opposite case
# Again, we return a list, because sapply does not seem 
# to play well with data frames... (try it!)
res <- sapply(IDs, function(i)
  {
  chosen.ID <- sample(as.list(i), 1)
  df[df$ID==chosen.ID,]
  }, simplify=FALSE)

# Finally convert the list to a data frame
res <- do.call(rbind, res)


> res
    ID Site Species
24 106    4       d
25 106    4       d
26 106    4       b
27 106    4       d
28 106    4       c
29 106    4       b
30 106    4       c
31 106    4       d
32 106    4       a
35 108    5       b
36 108    5       b
37 108    5       e
38 108    5       e
44 110    6       d
45 110    6       b
46 110    6       b
47 110    6       a
48 110    6       a
49 110    6       a

So, everything in a single function

pickSites <- function(df, num.sites)
    {
    all.sites <- unique(df$Site)
    chosen.sites <- sample(all.sites, num.sites)

    IDs <- sapply(chosen.sites, function(s)
        {
        unique(df$ID[df$Site==s])
        }, simplify=FALSE)

    res <- sapply(IDs, function(i)
        {
        chosen.ID <- sample(as.list(i), 1)
        df[df$ID==chosen.ID,]
        }, simplify=FALSE)

    res <- do.call(rbind, res)
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top