0

I have a dataframe (df1) which contains sensor data by user (approx 8-10 entries/rows for each user). I would like to automatically (i.e. not have to specify each username in the code, but rather just specify the column within which the grouping variables reside - in this case the userID) break down this dataframe into a series of smaller dataframes, based upon the userID. So I'd be left with lots of smaller dataframes, each of which contain all the data specific to a user.

I am aware of several other similar questions on here but I've been through all that I could find and none have worked for me so far (e.g. Pandas does not run on my version of R), or they are not relevant to my specific situation (e.g. they require individually typing out all the variable names to be grouped).

A sample of my dataframe is below (the actual dataframe is far larger, and some users have a few rows associated with the whilst others have many more):

UserID Street Data Speed
0001 Melrose 60 25
0001 Melrose 60 70
0002 Stamford 60 60
0002 Stamford 60 70
0001 Melrose 55 70
0001 Melrose 60 80
0003 Carpten 40 70
0003 Carpten 40 70
0001 Melrose 60 90

So assuming there are far more UserIDs in the column, some of which may be out of sequence (but all are unique to the user), is there a way I can break down the above dataframe into a series of smaller dataframes, each corresponding to the grouped UserID, similarly to the following?

DFa:

UserID Street Data Speed
0001 Melrose 60 25
0001 Melrose 60 70
0001 Melrose 60 90
0001 Melrose 55 70
0001 Melrose 60 80

DFb:

UserID Street Data Speed
0002 Stamford 60 60
0002 Stamford 60 70

DFc:

UserID Street Data Speed
0003 Carpten 40 70
0003 Carpten 40 70

EDIT: To clarify, this differs from this question (Split dataframe into multiple output files) because I don't want to have to specify each UserID and I do not know how many rows correspond to each UserID. Many thanks!

Browny
  • 27
  • 1
  • 6
  • 1
    Split by userid? I think `tidyr` or some `tidyverse` package also has `group_split`? – NelsonGon Nov 09 '21 at 22:41
  • 1
    Use `split(df1, f = df1$UserID)` to get a list of smaller data.frames for each `UserID`. You could use the `purrr` package to progress further. – Martin Gal Nov 09 '21 at 22:51

0 Answers0