split

Basics

split is a function with arguments x, a vector or data.frame, and f, a factor vector that will divide the data into smaller groups.

A useful optional argument is drop, which indicates whether or not values not in a group should be removed. The default is drop = FALSE.


Examples

How do I divide a vector into different groups?

Click to see solution
data <- c(5, 6, 8, 2, 1, 2, 18, 19)
groups <- c('A', 'A', 'A', 'B', 'C', 'C', 'C', 'C')
split(x = data, f = groups)
$A
[1] 5 6 8

$B
[1] 2

$C
[1]  1  2 18 19

How do I split a data.frame into groups?

Click to see solution
df <- data.frame(Product=c('X', 'X', 'Y', 'Y', 'Y', 'Z'),
                 Condition=c('T', 'T', 'F', 'F', 'T', 'F'),
                 Score=c(303, 128, 341, 319, 54, 74),
                 Quality=c(38, 27, 224, 228, 32, 41))
df
  Product Condition Score Quality
1       X         T   303      38
2       X         T   128      27
3       Y         F   341     224
4       Y         F   319     228
5       Y         T    54      32
6       Z         F    74      41
split(df, f = df$Product)
$X
  Product Condition Score Quality
1       X         T   303      38
2       X         T   128      27

$Y
  Product Condition Score Quality
3       Y         F   341     224
4       Y         F   319     228
5       Y         T    54      32

$Z
  Product Condition Score Quality
6       Z         F    74      41

How can I convert two vectors to a data.frame?

Click to see solution
Letters <- c("a", "b", "c", "a", "b", "c", "a", "b", "c")
Numbers <- c(1, 2, 3, 15, 40, 5, 33, 88, 7)

#splitting by name and converting to data.frame
as.data.frame(split(Numbers, Letters))
   a  b c
1  1  2 3
2 15 40 5
3 33 88 7