Reputation: 45
Using my dataframe below and working in R, I would like to generate a list of new dataframes in which each dataframe is based on a unique combination of 4 different values of my Taxon vector. In the dataframe below I have 10 different Taxon (many have multiple replicate entries) and I would like to select 4 Taxon at a time, for all combinations of 4. I believe that there should be 210 different combinations of 4 Taxon from these 10 Taxon (when sampled without replacement and order is not important). So ultimately I would like a list containing 210 dataframes, each containing a different combination of 4 Taxon (with all replicate rows for each Taxon)!!!
Though selection is based on the Taxon vector, I would like the new dataframes to include the other columns of information. For instance, if "Aphididae.sp.3" is selected, i would like the new dataframe to also have -25.92(C), 1.69(N), sap(func.group), herbivore(trophic.group) listed in columns alongside.
So far I used the "combn" function to generate all combinations of 4 taxon using the "unique" command, but I can't get all the other columns of information included in this, and it does not give all the replicate entries for each taxon! I'd be grateful for any help!
my code:
combos<-combn(unique(df$Taxon),4)
df:
Taxon C N func.group trophic.grp
1 Aphididae.sp.3 -25.92 1.69 sap herbivore
2 Aphididae.sp.3 -25.91 1.78 sap herbivore
3 Aphididae.sp.3 -26.05 1.74 sap herbivore
4 Cicadellidae.mixed.juvs -28.94 1.19 sap herbivore
5 Cicadellidae.mixed.juvs -29.25 2.24 sap herbivore
6 Cicadellidae.mixed.juvs -28.17 1.88 sap herbivore
7 Cicadellidae.mixed.juvs -28.29 1.94 sap herbivore
8 Cicadellidae.sp.1 -27.69 2.25 sap herbivore
9 Cicadellidae.sp.1 -27.67 2.41 sap herbivore
10 Cicadellidae.sp.1 -26.65 3.26 sap herbivore
11 Cicadellidae.sp.1 -28.30 3.20 sap herbivore
12 Cicadellidae.sp.1 -28.08 1.88 sap herbivore
13 Cicadellidae.sp.2 -26.59 2.89 sap herbivore
14 Cicadellidae.sp.3 -26.82 5.16 sap herbivore
15 Cicadellidae.sp.4 -26.54 3.46 sap herbivore
16 Cicadellidae.sp.4 -26.55 4.05 sap herbivore
17 Cicadellidae.sp.4 -27.20 3.14 sap herbivore
18 Cicadellidae.sp.4 -26.48 3.80 sap herbivore
19 Cicadellidae.sp.5 -27.54 4.17 sap herbivore
20 Cicadellidae.sp.5 -27.18 3.43 sap herbivore
21 Cicadellidae.sp.5 -27.46 4.03 sap herbivore
22 Cicadellidae.sp.6 -26.71 1.09 sap herbivore
23 Cicadellidae.sp.6 -26.33 1.56 sap herbivore
24 Cicadellidae.sp.6 -25.59 0.59 sap herbivore
25 Cicadellidae.sp.6 -25.07 0.84 sap herbivore
26 Cicadellidae.sp.6 -26.56 0.97 sap herbivore
27 Cicadellidae.sp.7 -25.84 1.08 sap herbivore
28 Cicadellidae.sp.7 -24.96 1.36 sap herbivore
29 Cicadellidae.sp.7 -26.15 1.90 sap herbivore
30 Cicadellidae.sp.7 -26.58 2.63 sap herbivore
31 Cicadellidae.sp.8 -28.02 2.28 sap herbivore
32 Cicadellidae.sp.8 -27.90 2.01 sap herbivore
33 Cicadellidae.sp.8 -27.70 1.92 sap herbivore
34 Cicadellidae.sp.8 -26.85 1.04 sap herbivore
Upvotes: 1
Views: 4584
Reputation: 263301
combos
should be a 4 x 210 matrix of character vectors. If you instead of your original code use:
combos<-combn(unique(as.character(df$Taxon)), 4) # factors would be converted
You should be able to pass those vectors to a subsetting function with apply
:
combdf <- apply(combos, 2, function(vec) df[ df$Taxon %in% vec, ] )
During testing I discovered that the original matrix got messed up because I had left the Taxons in as factors, hence the need for the as.character
call before unique
. I didn't see this until attempting the apply
call since I got 210 items but most of them were empty.
Upvotes: 2
Reputation: 13363
lapply(ncol(combos), function(x) df[df$Taxon %in% combos[,x],])
Upvotes: 1