# Thread: Is there a single function that creates as many subsets as you want?

1. ## Is there a single function that creates as many subsets as you want?

Hello,

I have a large dataset that is set up like so:

Site Flow Conc.1 Conc.2 etc.
1
1
1
2
2
3
3
3
4
4
5
6
6

The other columns are also filled with data. I would like to separately analyze each site, but there are a large number of sites. Is there a function that can look at the column "sites" and create a subset of data for each site?

Thank you,

Jake

2. ## Re: Is there a single function that creates as many subsets as you want?

You can use split to put each of the subsets of the data into their own dataframe that is stored in a list.

Here's an example
Code:
``````# Look at mtcars, notice there are only a few values for carb
mtcars
# Split the data based on the value of carb
split(mtcars, mtcars\$carb)``````

3. ## Re: Is there a single function that creates as many subsets as you want?

Thank you, Dason.

I have a followup question: Is there a function or series of functions that can automatically - for each site that I've just split from the main dataset - run each column against every other column?

I've done the ggplot scatterplotMatrix on my entire dataset, but I want to do it for each individual site. Does this make sense? I'm very new to R and still am not familiar with many of the basic functions.

4. ## Re: Is there a single function that creates as many subsets as you want?

lapply will apply a function to each element of a list. So you could use lapply on the output from split. You would need to write a function that would take as input a subset of interest and produce the plot you want.

Code:
``````dat <- split(mtcars, mtcars\$carb)

# Applies "summary" to each of the subsets
lapply(dat, summary)``````

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts