How to split a data frame?

后端 未结 8 2217
臣服心动
臣服心动 2020-11-22 03:08

I want to split a data frame into several smaller ones. This looks like a very trivial question, however I cannot find a solution from web search.

8条回答
  •  感情败类
    2020-11-22 03:37

    I just posted a kind of a RFC that might help you: Split a vector into chunks in R

    x = data.frame(num = 1:26, let = letters, LET = LETTERS)
    ## number of chunks
    n <- 2
    dfchunk <- split(x, factor(sort(rank(row.names(x))%%n)))
    dfchunk
    $`0`
       num let LET
    1    1   a   A
    2    2   b   B
    3    3   c   C
    4    4   d   D
    5    5   e   E
    6    6   f   F
    7    7   g   G
    8    8   h   H
    9    9   i   I
    10  10   j   J
    11  11   k   K
    12  12   l   L
    13  13   m   M
    
    $`1`
       num let LET
    14  14   n   N
    15  15   o   O
    16  16   p   P
    17  17   q   Q
    18  18   r   R
    19  19   s   S
    20  20   t   T
    21  21   u   U
    22  22   v   V
    23  23   w   W
    24  24   x   X
    25  25   y   Y
    26  26   z   Z
    

    Cheers, Sebastian

提交回复
热议问题