Skip to content
Prev 360156 / 398503 Next

a replace for subset

Use the split function to automatically create a list of pre-subsetted 
data frames, and then generate your output however you wish to. For 
example (using Jim Lemon's sample data generator):

library(ggplot2)

mydata <- data.frame( RE = sample( 5:50, 100, TRUE)
                     , LU = sample( 1500:4500, 100 )
                     , COUNTRY = factor( sample( c( "DE","FR","JP","AU")
                                               , 100
                                               , TRUE
                                               )
                                       )
                     , Light = factor( sample( c( "ON", "OFF" )
                                             , 100
                                             , TRUE
                                             )
                                     )
                     , OR = factor( sample( c( "S", "T" )
                                          , 100
                                          , TRUE
                                          )
                                  )
                     , PAT = factor( sample( c( "low", "high", "middle" )
                                           ,100
                                           ,TRUE
                                           )
                                   )
                     )
# split wants you to specify a list of columns to create unique
# groups by;
# data frames are lists of columns;
# data frame indexing lets you specify a subset of columns
mydataList0 <- split( mydata
                     , mydata[ , c( "COUNTRY", "Light" ) ]
                     )
# you should use the str() function frequently in an interactive
# fashion to help you understand the data you are working with:
str( mydataList0 )

# if you try to specify a single column as a subset of columns,
# R will by default forget the "list of" aspect... to keep it, use 
# drop=FALSE
mydataList <- split( mydata
                    , mydata[ , c( "COUNTRY" ), drop = FALSE ]
                    )

# I happen to like packing information into a single plot where possible.
# Since you did not provide a minimial reproducible example, I cannot
# tell whether this will work for you. You can use some variant of 
# mydataList0 if you don't like this approach.
for ( idx in seq_along( mydataList ) ) {
     print( ggplot( mydataList[[ idx ]], aes( x=RE, y=LU, shape=Light ) ) +
             geom_point() +
             facet_grid( PAT ~ OR ) +
             ggtitle( paste( "Country ="
                           , mydataList[[ idx ]][1,"COUNTRY"]))
     )
}

For future reference, the Posting Guide mentions several good practices 
for asking questions online that will help you understand your own problem 
better as well as making it easier for us to provide answers.
On Sat, 16 Apr 2016, ch.elahe via R-help wrote:

            
---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<jdnewmil at dcn.davis.ca.us>        Basics: ##.#.       ##.#.  Live Go...
                                       Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k