Skip to content
Prev 348263 / 398500 Next

Coding style question

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