df <- data.frame(matrix(rnorm(40), nrow=20))
df$color <-  rep(c("blue", "red", "yellow", "pink"), each=5)
df
##              X1          X2  color
## 1  -0.157715245 -0.29825790   blue
## 2   1.125197042  1.41231281   blue
## 3  -0.743180496  0.22592148   blue
## 4   0.683271044 -1.10097279   blue
## 5  -0.202818522 -1.33263880   blue
## 6  -0.005907518  0.97573473    red
## 7   1.259831274  1.49417068    red
## 8  -1.176965639  0.04541681    red
## 9   0.471804542  0.57761261    red
## 10  0.752084224  1.98040183    red
## 11  0.968195735  1.34588394 yellow
## 12 -0.264085376 -0.20997119 yellow
## 13 -0.992257901  0.86845112 yellow
## 14  1.703114282 -0.80103362 yellow
## 15 -0.430625695  1.84311418 yellow
## 16  0.839865478  0.16643782   pink
## 17 -1.015359608  1.02882835   pink
## 18 -1.749668104  0.13943729   pink
## 19  0.867042638 -0.51064052   pink
## 20 -1.280993516  0.22904877   pink
df2 <- lapply(split(df, df$color),
              function(x) x[1,])
class(split(df, df$color)); class(df2)
## [1] "list"
## [1] "list"
df2
## $blue
##           X1         X2 color
## 1 -0.1577152 -0.2982579  blue
## 
## $pink
##           X1        X2 color
## 16 0.8398655 0.1664378  pink
## 
## $red
##             X1        X2 color
## 6 -0.005907518 0.9757347   red
## 
## $yellow
##           X1       X2  color
## 11 0.9681957 1.345884 yellow
do.call('rbind', df2)
##                  X1         X2  color
## blue   -0.157715245 -0.2982579   blue
## pink    0.839865478  0.1664378   pink
## red    -0.005907518  0.9757347    red
## yellow  0.968195735  1.3458839 yellow
lapply(split(df, df$color),
       function(subdf) subdf[sample(1:nrow(subdf), 2),])
## $blue
##           X1         X2 color
## 1 -0.1577152 -0.2982579  blue
## 4  0.6832710 -1.1009728  blue
## 
## $pink
##           X1         X2 color
## 19 0.8670426 -0.5106405  pink
## 16 0.8398655  0.1664378  pink
## 
## $red
##             X1        X2 color
## 6 -0.005907518 0.9757347   red
## 9  0.471804542 0.5776126   red
## 
## $yellow
##            X1         X2  color
## 15 -0.4306257  1.8431142 yellow
## 14  1.7031143 -0.8010336 yellow
### Split a matrix into a list by columns
ma <- cbind(x = 1:10, y = (-4:5)^2)
split(ma, col(ma))
## $`1`
##  [1]  1  2  3  4  5  6  7  8  9 10
## 
## $`2`
##  [1] 16  9  4  1  0  1  4  9 16 25
split(1:10, 1:5)
## $`1`
## [1] 1 6
## 
## $`2`
## [1] 2 7
## 
## $`3`
## [1] 3 8
## 
## $`4`
## [1] 4 9
## 
## $`5`
## [1]  5 10
split(1:10, 4:5)
## $`4`
## [1] 1 3 5 7 9
## 
## $`5`
## [1]  2  4  6  8 10
#Sample n random rows per group in a dataframe