[R] Coding style question

John Posner john.posner at MJBIOSTAT.COM
Tue Feb 17 17:19:57 CET 2015


In the course of slicing-and-dicing some data, I had occasion to create a list like this:

list(
    subset(my_dataframe, GR1=="XX1"),
    subset(my_dataframe, GR1=="XX2"),
    subset(my_dataframe, GR1=="YY"),
    subset(my_dataframe, GR1 %in% c("XX1", "XX2")), 
    subset(my_dataframe, GR2=="Remission"),
    subset(my_dataframe, GR2=="Relapse"))

I used %in% only once, because there was only one "compound value" (XX1 or XX2) for subsetting. But then it occurred to me to use %in% everywhere, taking advantage of the fact that a scalar value is the same as a length-1 vector:

list(
    subset(my_dataframe, GR1 %in% "XX1"),
    subset(my_dataframe, GR1 %in% "XX2"),
    subset(my_dataframe, GR1 %in% "YY"),
    subset(my_dataframe, GR1 %in% c("XX1", "XX2")),
    subset(my_dataframe, GR2 %in% "Remission"),
    subset(my_dataframe, GR2 %in% "Relapse"))

It works just fine.  Are there any problems with this style, from the standpoints of correctness, aesthetics, etc.?

-John



More information about the R-help mailing list