Split dataframe using two columns of data and apply common transformation on list of resulting dataframes

前端 未结 2 1464
余生分开走
余生分开走 2020-12-01 10:07

I want to split a large dataframe into a list of dataframes according to the values in two columns. I then want to apply a common data transformation on all dataframes (lag

2条回答
  •  借酒劲吻你
    2020-12-01 10:46

    You need to put all the factors you want to split by in a list, eg:

    split(mtcars,list(mtcars$cyl,mtcars$gear))
    

    Then you can use lapply on this to do what else you want to do.

    If you want to avoid having zero row dataframes in the results, there is a drop parameter whose default is the opposite of the drop parameter in the "[" function.

    split(mtcars,list(mtcars$cyl,mtcars$gear), drop=TRUE)
    

提交回复
热议问题