How to split a data frame?

后端 未结 8 2627
臣服心动
臣服心动 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:21

    If you want to split a dataframe according to values of some variable, I'd suggest using daply() from the plyr package.

    library(plyr)
    x <- daply(df, .(splitting_variable), function(x)return(x))
    

    Now, x is an array of dataframes. To access one of the dataframes, you can index it with the name of the level of the splitting variable.

    x$Level1
    #or
    x[["Level1"]]
    

    I'd be sure that there aren't other more clever ways to deal with your data before splitting it up into many dataframes though.

提交回复
热议问题