using R data.table with 'all()' to subset data -


i have following data.table:

  dt=structure(list(a = c("10", "10", "20", "30", "10", "25", "10"     ), b = c("0.605887455840394", "0", "0.709466017509524", "0",      "0.585528817843856", "-0.109303314681054", "-0.453497173462763"     ), c = c("-0.919322002474128", "0", "0.630098551068391", "0",      "-1.81795596770373", "-0.276184105225216", "-0.284159743943371"     ), d = c("-0.750531994502331", "0", "1.81731204370422", "0",      "-0.116247806352002", "0.370627864257954", "0.520216457554957"     ), e = c("0.298723699267293", "0", "-0.886357521243213", "0",      "0.816899839520583", "-0.331577589942552", "1.12071265166956"     ), key = c("a", "a", "b", "b", "c", "c", "c")), .names = c("a",      "b", "c", "d", "e", "key"), row.names = c(na, -7l), class = c("data.table",          "data.frame"), sorted = "key") 

which gives me datatable similar shown below.

                     b                  c                  d                  e key 1: 10  0.605887455840394 -0.919322002474128 -0.750531994502331  0.298723699267293   2: 10                  0                  0                  0                  0   3: 20  0.709466017509524  0.630098551068391   1.81731204370422 -0.886357521243213   b 4: 30                  0                  0                  0                  0   b 5: 10  0.585528817843856  -1.81795596770373 -0.116247806352002  0.816899839520583   c 6: 25 -0.109303314681054 -0.276184105225216  0.370627864257954 -0.331577589942552   c 7: 10 -0.453497173462763 -0.284159743943371  0.520216457554957   1.12071265166956   c 

i'd subsetting operation removes rows zeros.

i thinking of along lines of

dt[!(all(i[2:4) == 0)] i'm not sure how state in data.table

would grateful this.

this two-step solution:

dt[     !dt[,         .i[all(sapply(.sd,function(x)x=="0"))]     ,by=1:nrow(dt),.sdcols=letters[2:5]]$v1 ] 

yielding

                     b                  c                  d                  e key 1: 10  0.605887455840394 -0.919322002474128 -0.750531994502331  0.298723699267293   2: 20  0.709466017509524  0.630098551068391   1.81731204370422 -0.886357521243213   b 3: 10  0.585528817843856  -1.81795596770373 -0.116247806352002  0.816899839520583   c 4: 25 -0.109303314681054 -0.276184105225216  0.370627864257954 -0.331577589942552   c 5: 10 -0.453497173462763 -0.284159743943371  0.520216457554957   1.12071265166956   c 

the inner part selects row indices ".i" satisfying condition. outer bracket subsets "dt" excluding rows using not "!" operator.


Comments

Popular posts from this blog

.htaccess - First slash is removed after domain when entering a webpage in the browser -

Automatically create pages in phpfox -

c# - Farseer ContactListener is not working -