r - How to apply indexing to paired rows -


i have following data frame representing paired observations made each id:

      structure(list(id = c(9000099, 9000099, 9000296, 9000296, 9000622,        9000622), variable = c(2, 0, 0, 4, 0, 1), side = c(1, 2, 1, 2,        1, 2)), .names = c("id", "variable", "side"), row.names = c(na,        6l), class = "data.frame")            id variable side     1 9000099        2    1     2 9000099        0    2     3 9000296        0    1     4 9000296        4    2     5 9000622        0    1     6 9000622        1    2 

i index dataframe if variable meets indexing criteria anywhere, both rows corresponding id removed. example, if index data frame variable = 4, df like:

            id variable side     1 9000099        2    1     2 9000099        0    2     5 9000622        0    1     6 9000622        1    2 

if index variable = 1, data frame follows:

          id variable side     1 9000099        2    1     2 9000099        0    2     3 9000296        0    1     4 9000296        4    2 

is there way without transforming data frame?

you can write small function this:

indexer <- function(value) {   dropme <- unique(df[df$variable %in% value, "id"])   df[!df$id %in% dropme, ] } 

to apply it, do:

indexer(4) #        id variable side # 1 9000099        2    1 # 2 9000099        0    2 # 5 9000622        0    1 # 6 9000622        1    2 indexer(1) #        id variable side # 1 9000099        2    1 # 2 9000099        0    2 # 3 9000296        0    1 # 4 9000296        4    2 indexer(c(2, 4)) #        id variable side # 5 9000622        0    1 # 6 9000622        1    2 

of course, function unique data.frame have shared. should modify can specify dataset name, "variable" column, , id column. example:

subsetter <- function(indf, value, look.in, group) {   dropme <- unique(indf[df[[look.in]] %in% value, group])   indf[!indf[[group]] %in% dropme, ] } 

try out:

subsetter(df, 1, look.in = "variable", group = "id") 

Comments

Popular posts from this blog

matlab - "Contour not rendered for non-finite ZData" -

delphi - Indy UDP Read Contents of Adata -

javascript - Any ideas when Firefox is likely to implement lengthAdjust and textLength? -