[R] Fwd: Conditional inclusion of an element in an R object

Jeff Newmiller jdnewmil at dcn.davis.CA.us
Sat Jan 11 03:16:25 CET 2014

I agree with Don... focus on identifying the names of the columns and then use column name indexing to extract the columns you want. You will probably want to rename them to a standard set of names once you have gone to all this trouble... just assign the new vector of names to the names function of the extracted data frame.

Using nested if-else may be messy, but regardless of whether you figure out a better way to identify columns that works for your data, you should be able to using indexing by name both now and later.
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
Sent from my phone. Please excuse my brevity.

"MacQueen, Don" <macqueen1 at llnl.gov> wrote:
>At the risk of being annoying ...
>Your original question was,
> "Is there a way to dynamically include columns in a dataframe?"
>The answer is yes. One way, and I think the simplest, is to calculate
>names of the columns you want to keep, and then use an expression like
>suggested, that is, one like
>  a1[ , names.to.keep]
>instead of using select().
>Or calculate the names  you do not want to keep, if that is easier, and
>use for example
>  a1[, setdiff(names(a1),names.to.drop) ]
>Based on your most recent email it seems like calculating which columns
>keep or drop may be difficult. But I would still suggest a better
>would be to focus on calculating a character vector of column names.
>you can convert your lc1 and lc2 objects to vectors of column names.
>Speaking personally, nested ifelse() expressions make me want to get up
>and run away. So I've very reluctant to put any effort into trying to
>figure out what they produce. But that's just my preference; others may
>feel differently.

More information about the R-help mailing list