-1

I'm trying to split a dataframe several times and I'm having a bit of trouble doing that. I've been trying to do this with the code below and it works for the first line of code but it doesn't go any further and I haven't quite been able to figure it out:

rs_s <- list2env(split(regular_season, regular_season$season), 
        .GlobalEnv)
rs_sc <- list2env(split(rs_s, rs_s$category), 
        .GlobalEnv)
rs_scq - list2env(split(rs_sc, rs_sc$question), 
        .GlobalEnv)

As I understand it, the entries in the split() function are supposed to be data frames and I have a feeling that "rs_sc" and "rs_scq" would not be considered data frames and that I might have to use a for loop but I'm not quite sure about this. If anyone has any idea, can you please help me?

EDIT: As an output, I'm hoping to have a set of mini-data frames that is indexed by the elements of "season," "category," and "question." The data frames I'm working with are much larger, but as an example, if "season" consisted of two elements

spring, fall

and "category" consisted of three elements

history, math, english

and "question" consisted of three elements

who, what, why

I'm hoping to end up with a set consisting of 18 data frames (2 seasons x 3 categories x 3 questions). If it would affect anything, my end goal right now of getting this set is to be able to run statistical regressions within the individual data frames

Aaron
  • 181
  • 5
  • 1
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. Usually it's better to leave data in lists rather than pollute your global environment with a bunch of objects with data in their name. Lists are much easier to work with in R than variables in an environment. – MrFlick May 11 '21 at 01:32

2 Answers2

0

You can pass multiple column values in split to get list of dataframes.

result <- split(regular_season, list(regular_season$season,
                regular_season$category, regular_season$question))

For example -

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

#$`4.0`
#               mpg cyl  disp hp drat    wt  qsec vs am gear carb
#Merc 240D     24.4   4 146.7 62 3.69 3.190 20.00  1  0    4    2
#Merc 230      22.8   4 140.8 95 3.92 3.150 22.90  1  0    4    2
#Toyota Corona 21.5   4 120.1 97 3.70 2.465 20.01  1  0    3    1

#$`6.0`
#                mpg cyl  disp  hp drat    wt  qsec vs am gear carb
#Hornet 4 Drive 21.4   6 258.0 110 3.08 3.215 19.44  1  0    3    1
#Valiant        18.1   6 225.0 105 2.76 3.460 20.22  1  0    3    1
#Merc 280       19.2   6 167.6 123 3.92 3.440 18.30  1  0    4    4
#Merc 280C      17.8   6 167.6 123 3.92 3.440 18.90  1  0    4    4

#$`8.0`
#                     mpg cyl  disp  hp drat    wt  qsec vs am gear carb
#Hornet Sportabout   18.7   8 360.0 175 3.15 3.440 17.02  0  0    3    2
#Duster 360          14.3   8 360.0 245 3.21 3.570 15.84  0  0    3    4
#Merc 450SE          16.4   8 275.8 180 3.07 4.070 17.40  0  0    3    3
#Merc 450SL          17.3   8 275.8 180 3.07 3.730 17.60  0  0    3    3
#Merc 450SLC         15.2   8 275.8 180 3.07 3.780 18.00  0  0    3    3
#Cadillac Fleetwood  10.4   8 472.0 205 2.93 5.250 17.98  0  0    3    4
#Lincoln Continental 10.4   8 460.0 215 3.00 5.424 17.82  0  0    3    4
#Chrysler Imperial   14.7   8 440.0 230 3.23 5.345 17.42  0  0    3    4
#Dodge Challenger    15.5   8 318.0 150 2.76 3.520 16.87  0  0    3    2
#AMC Javelin         15.2   8 304.0 150 3.15 3.435 17.30  0  0    3    2
#Camaro Z28          13.3   8 350.0 245 3.73 3.840 15.41  0  0    3    4
#Pontiac Firebird    19.2   8 400.0 175 3.08 3.845 17.05  0  0    3    2
#...
#...
#...
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
0

The data.frame is a list of columns. So, we don't need to specifically extract columns and then wrap in a list

split(mtcars, mtcars[c("cyl", "am")], drop = TRUE)

-output

#$`4.0`
#               mpg cyl  disp hp drat    wt  qsec vs am gear carb
#Merc 240D     24.4   4 146.7 62 3.69 3.190 20.00  1  0    4    2
#Merc 230      22.8   4 140.8 95 3.92 3.150 22.90  1  0    4    2
#Toyota Corona 21.5   4 120.1 97 3.70 2.465 20.01  1  0    3    1

#$`6.0`
#                mpg cyl  disp  hp drat    wt  qsec vs am gear carb
#Hornet 4 Drive 21.4   6 258.0 110 3.08 3.215 19.44  1  0    3    1
#Valiant        18.1   6 225.0 105 2.76 3.460 20.22  1  0    3    1
#Merc 280       19.2   6 167.6 123 3.92 3.440 18.30  1  0    4    4
#Merc 280C      17.8   6 167.6 123 3.92 3.440 18.90  1  0    4    4

#$`8.0`
#                     mpg cyl  disp  hp drat    wt  qsec vs am gear carb
#Hornet Sportabout   18.7   8 360.0 175 3.15 3.440 17.02  0  0    3    2
#Duster 360          14.3   8 360.0 245 3.21 3.570 15.84  0  0    3    4
#Merc 450SE          16.4   8 275.8 180 3.07 4.070 17.40  0  0    3    3
#Merc 450SL          17.3   8 275.8 180 3.07 3.730 17.60  0  0    3    3
#Merc 450SLC         15.2   8 275.8 180 3.07 3.780 18.00  0  0    3    3
# ..
akrun
  • 874,273
  • 37
  • 540
  • 662