Split a large dataframe into a list of data frames based on common value in column

后端 未结 3 938
离开以前
离开以前 2020-11-22 07:52

I have a data frame with 10 columns, collecting actions of \"users\", where one of the columns contains an ID (not unique, identifying user)(column 10). the length of the da

3条回答
  •  南笙
    南笙 (楼主)
    2020-11-22 08:46

    Stumbled across this answer and I actually wanted BOTH groups (data containing that one user and data containing everything but that one user). Not necessary for the specifics of this post, but I thought I would add in case someone was googling the same issue as me.

    df <- data.frame(
         ran_data1=rnorm(125),
         ran_data2=rnorm(125),
         g=rep(factor(LETTERS[1:5]), 25)
     )
    
    test_x = split(df,df$g)[['A']]
    test_y = split(df,df$g!='A')[['TRUE']]
    

    Here's what it looks like:

    head(test_x)
                x          y g
    1   1.1362198  1.2969541 A
    6   0.5510307 -0.2512449 A
    11  0.0321679  0.2358821 A
    16  0.4734277 -1.2889081 A
    21 -1.2686151  0.2524744 A
    
    > head(test_y)
                x          y g
    2 -2.23477293  1.1514810 B
    3 -0.46958938 -1.7434205 C
    4  0.07365603  0.1111419 D
    5 -1.08758355  0.4727281 E
    7  0.28448637 -1.5124336 B
    8  1.24117504  0.4928257 C
    

提交回复
热议问题