What does `split()` function do?

I wonder how split() function binning data. I mean, based on what?

If i have 100 random natural numbers defined by x, and then split it into group 1 and group 2
split(x, factor(c(1,2)))
How it decides the data (let say, number 4) will go to one of the two groups (1 or 2)? I don't get enough information by searching in the internet

I can't say that it splitting randomly because it always returns the same result without using set.seed() function

split() uses the factor to assign elements of x to the elements of the list returned by split(). When the factor is not the same length as x, the factor is recycled to match the length of x. If the length of x is an integer multiple of the length of the factor, the recycling is done silently, which is what you see with x having 100 elements and the factor having 2 elements.

Here are examples. You can see in the first part that the letter A is in position 1, 3, 7 and 10, so the first, seventh and tenth elements of x are assigned to the A element of the list. In the second part, the factor has length 2 and x has length 19 and a warning is raised, though the assignments are completed.

x <- 1:10
FAC <- c("A", "C", "A", "B", "B", "C", "A","C", "B", "A")
x
#>  [1]  1  2  3  4  5  6  7  8  9 10
FAC
#>  [1] "A" "C" "A" "B" "B" "C" "A" "C" "B" "A"
split(x, FAC)
#> $A
#> [1]  1  3  7 10
#> 
#> $B
#> [1] 4 5 9
#> 
#> $C
#> [1] 2 6 8

x2 <- 1:19
FAC2 <- 1:2

split(x2, FAC2)
#> Warning in split.default(x2, FAC2): data length is not a multiple of split
#> variable
#> $`1`
#>  [1]  1  3  5  7  9 11 13 15 17 19
#> 
#> $`2`
#> [1]  2  4  6  8 10 12 14 16 18

Created on 2020-05-30 by the reprex package (v0.2.1)

1 Like

Crystal clear.
Thank you.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.