+ Reply to Thread
Results 1 to 4 of 4

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

  1. #1
    Points: 6, Level: 1
    Level completed: 11%, Points required for next Level: 44

    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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. #2
    RotParaTon
    Points: 47,119, Level: 100
    Level completed: 0%, Points required for next Level: 0
    Awards:
    Discussion EnderPosting AwardCommunity AwardMaster TaggerFrequent Poster
    Dason's Avatar
    Location
    Ames, IA
    Posts
    9,185
    Thanks
    212
    Thanked 1,639 Times in 1,401 Posts

    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)
    "His programming is malfunctioning. It begins! Get your weapons, he's going to become a killbot!!!" - bryangoodrich

  3. #3
    Points: 6, Level: 1
    Level completed: 11%, Points required for next Level: 44

    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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. #4
    RotParaTon
    Points: 47,119, Level: 100
    Level completed: 0%, Points required for next Level: 0
    Awards:
    Discussion EnderPosting AwardCommunity AwardMaster TaggerFrequent Poster
    Dason's Avatar
    Location
    Ames, IA
    Posts
    9,185
    Thanks
    212
    Thanked 1,639 Times in 1,401 Posts

    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)
    "His programming is malfunctioning. It begins! Get your weapons, he's going to become a killbot!!!" - bryangoodrich

+ Reply to Thread

Posting Permissions

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








Advertise on Talk Stats