[R] Removing rows with rowsums==0 (I can't figure this out)
Gavin Simpson
gavin.simpson at ucl.ac.uk
Thu Nov 20 18:28:22 CET 2008
On Thu, 2008-11-20 at 17:08 +0000, Gavin Simpson wrote:
> On Thu, 2008-11-20 at 12:01 -0500, stephen sefick wrote:
> > ##I want to remove the rows where the row sums are zero and this is as
> > far as I have gotten
>
> Given your ffg,
>
> ## the which() call returns row indices for rows with rowSum > 0
> ffg[which(rowSums(ffg) > 0, ]
^^^^
Agghh. Typo, missing parenthesis above. Should have read
ffg[which(rowSums(ffg) > 0), ]
But Prof. Ripley has pointed out (off list) that
ffg[rowSums(ffg) > 0, ]
is really all that is required.
G
>
> does the trick
>
> HTH
>
> G
>
> >
> > ffg <- (structure(list(CD = c(0, 0, 0, 0, 3.125, 0, 0, 0, 0, 1.6, 3.125,
> > 0, 0, 6.25, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3.125, 0, 0, 0, 0,
> > 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1.6, 0, 0, 0, 0, 0,
> > 0, 0, 0, 0, 0, 0, 1.6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
> > 0, 0, 0, 0, 0, 3.125, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
> > 0, 0, 0, 0, 0, 12.5, 3.125, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
> > 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3.125, 9.375, 0, 0, 0, 0, 0, 0,
> > 0, 0, 0, 0, 0, 0, 0, 0), GC = c(123.2, 93.75, 12.5, 0, 56.25,
> > 184.375, 28.125, 109.375, 0, 779.2, 190.625, 59.375, 0, 1537.5,
> > 2340.625, 1603.125, 481.25, 334.375, 0, 0, 9.375, 312.5, 109.375,
> > 0, 1081.25, 1346.875, 553.125, 171.875, 78.125, 0, 0, 99.2, 221.875,
> > 3.125, 0, 871.875, 1809.375, 606.25, 71.875, 37.5, 0, 0, 0, 20.8,
> > 159.375, 59.375, 0, 84.375, 187.5, 84.375, 46.875, 37.5, 0, 0,
> > 0, 9.6, 75, 3.125, 0, 159.375, 150, 46.875, 143.75, 343.75, 0,
> > 0, 0, 120, 18.75, 6.25, 0, 181.25, 237.5, 28.125, 62.5, 128.125,
> > 0, 0, 0, 78.4, 0, 3.125, 3.125, 12.5, 6.25, 18.75, 0, 25, 103.125,
> > 68.75, 46.875, 0, 290.625, 225, 84.375, 62.5, 137.5, 0, 0, 0,
> > 252.8, 28.125, 9.375, 0, 796.875, 225, 37.5, 3.125, 431.25, 0,
> > 0, 0, 12.5, 21.875, 6.25, 0, 37.5, 296.875, 56.25, 65.625, 65.625,
> > 0, 0, 0, 25, 103.125, 0, 184.375, 346.875, 240.625, 46.875, 246.875
> > ), GF = c(118.4, 43.75, 9.375, 0, 9.375, 12.5, 9.375, 43.75,
> > 0, 190.4, 56.25, 18.75, 0, 31.25, 134.375, 325, 93.75, 25, 0,
> > 0, 0, 0, 0, 0, 2278.125, 1115.625, 1834.375, 762.5, 15.625, 0,
> > 0, 33.6, 31.25, 0, 0, 25, 543.75, 225, 3.125, 18.75, 0, 0, 0,
> > 17.6, 3.125, 3.125, 0, 0, 21.875, 0, 0, 3.125, 0, 0, 0, 0, 0,
> > 0, 0, 9.375, 0, 0, 6.25, 21.875, 0, 0, 0, 6.4, 0, 0, 0, 21.875,
> > 12.5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 62.5, 412.5,
> > 15.625, 0, 87.5, 100, 6.25, 6.25, 0, 0, 0, 0, 14.4, 3.125, 0,
> > 0, 90.625, 6.25, 0, 0, 506.25, 0, 0, 0, 25, 25, 0, 0, 0, 59.375,
> > 18.75, 31.25, 6.25, 0, 0, 0, 3.125, 0, 0, 37.5, 3.125, 6.25,
> > 0, 25), P = c(0, 0, 0, 0, 0, 0, 3.125, 0, 0, 3.2, 0, 0, 0, 0,
> > 0, 0, 0, 0, 0, 0, 12.5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 19.2, 0,
> > 0, 0, 0, 0, 6.25, 0, 0, 0, 0, 0, 6.4, 0, 0, 0, 6.25, 0, 0, 0,
> > 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3.125, 0, 0, 0, 0, 0, 3.125,
> > 0, 0, 0, 0, 0, 3.125, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
> > 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
> > 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
> > 9.375, 0), PR = c(27.2, 15.625, 18.75, 0, 21.875, 62.5, 78.125,
> > 25, 0, 92.8, 6.25, 9.375, 0, 68.75, 78.125, 100, 37.5, 106.25,
> > 0, 0, 31.25, 21.875, 0, 0, 150, 268.75, 534.375, 31.25, 3.125,
> > 0, 0, 43.2, 9.375, 0, 0, 21.875, 128.125, 87.5, 12.5, 3.125,
> > 0, 0, 0, 16, 12.5, 3.125, 0, 59.375, 65.625, 37.5, 6.25, 3.125,
> > 0, 0, 0, 12.8, 6.25, 0, 6.25, 28.125, 21.875, 9.375, 6.25, 28.125,
> > 0, 0, 0, 16, 15.625, 3.125, 0, 43.75, 75, 6.25, 12.5, 18.75,
> > 0, 0, 0, 1.6, 0, 0, 0, 3.125, 0, 0, 9.375, 0, 37.5, 25, 6.25,
> > 0, 28.125, 118.75, 53.125, 68.75, 40.625, 0, 0, 0, 72, 0, 0,
> > 0, 218.75, 71.875, 115.625, 9.375, 43.75, 0, 0, 0, 25, 3.125,
> > 18.75, 6.25, 34.375, 84.375, 31.25, 3.125, 15.625, 0, 0, 0, 12.5,
> > 3.125, 0, 53.125, 150, 56.25, 0, 6.25), SC = c(105.6, 93.75,
> > 200, 0, 256.25, 112.5, 31.25, 90.625, 0, 81.6, 68.75, 153.125,
> > 0, 103.125, 150, 71.875, 134.375, 165.625, 0, 0, 50, 65.625,
> > 6.25, 0, 6.25, 28.125, 6.25, 0, 3.125, 0, 0, 40, 0, 0, 0, 15.625,
> > 31.25, 40.625, 21.875, 0, 0, 0, 0, 11.2, 0, 0, 0, 40.625, 15.625,
> > 3.125, 3.125, 6.25, 0, 0, 0, 6.4, 15.625, 6.25, 0, 12.5, 50,
> > 18.75, 0, 0, 0, 0, 0, 22.4, 6.25, 18.75, 0, 43.75, 81.25, 25,
> > 6.25, 0, 0, 0, 0, 4.8, 0, 3.125, 0, 0, 0, 3.125, 0, 0, 56.25,
> > 434.375, 53.125, 0, 65.625, 84.375, 87.5, 106.25, 218.75, 0,
> > 0, 0, 1.6, 3.125, 0, 0, 0, 0, 6.25, 0, 3.125, 0, 0, 0, 56.25,
> > 81.25, 128.125, 0, 62.5, 62.5, 6.25, 9.375, 96.875, 0, 0, 0,
> > 0, 0, 0, 9.375, 12.5, 3.125, 0, 9.375), SH = c(25.6, 15.625,
> > 3.125, 0, 12.5, 21.875, 15.625, 100, 0, 94.4, 28.125, 6.25, 0,
> > 43.75, 43.75, 150, 25, 50, 0, 0, 6.25, 6.25, 190.625, 0, 34.375,
> > 184.375, 731.25, 259.375, 250, 0, 0, 67.2, 528.125, 6.25, 0,
> > 106.25, 890.625, 475, 6.25, 34.375, 0, 0, 0, 19.2, 134.375, 31.25,
> > 0, 137.5, 171.875, 12.5, 46.875, 62.5, 0, 0, 0, 30.4, 71.875,
> > 6.25, 12.5, 75, 206.25, 46.875, 15.625, 153.125, 0, 0, 0, 78.4,
> > 12.5, 115.625, 21.875, 131.25, 259.375, 6.25, 15.625, 75, 0,
> > 0, 0, 43.2, 21.875, 28.125, 3.125, 3.125, 131.25, 212.5, 131.25,
> > 87.5, 37.5, 37.5, 0, 0, 153.125, 56.25, 28.125, 150, 3.125, 0,
> > 0, 0, 92.8, 62.5, 18.75, 37.5, 53.125, 128.125, 231.25, 50, 140.625,
> > 0, 0, 0, 0, 0, 3.125, 12.5, 9.375, 18.75, 18.75, 3.125, 3.125,
> > 0, 0, 0, 6.25, 0, 15.625, 21.875, 31.25, 15.625, 28.125, 15.625
> > )), .Names = c("CD", "GC", "GF", "P", "PR", "SC", "SH"), class = c("cast_df",
> > "data.frame"), row.names = c("119 2006/10", "119 2006/12", "119 2007/02",
> > "119 2007/04", "119 2007/06", "119 2007/08", "119 2007/10", "119 2008/01",
> > "148 2006/05", "148 2006/10", "148 2006/12", "148 2007/02", "148 2007/04",
> > "148 2007/06", "148 2007/08", "148 2007/10", "148 2007/12", "148 2008/01",
> > "179 2006/04", "179 2006/05", "179 2006/10", "179 2006/12", "179 2007/02",
> > "179 2007/04", "179 2007/06", "179 2007/08", "179 2007/10", "179 2007/12",
> > "179 2008/01", "185 2006/04", "185 2006/07", "185 2006/10", "185 2006/12",
> > "185 2007/02", "185 2007/04", "185 2007/06", "185 2007/08", "185 2007/10",
> > "185 2007/12", "185 2008/01", "190 2006/04", "190 2006/05", "190 2006/07",
> > "190 2006/10", "190 2006/12", "190 2007/02", "190 2007/04", "190 2007/06",
> > "190 2007/08", "190 2007/10", "190 2007/12", "190 2008/01", "198 2006/04",
> > "198 2006/05", "198 2006/07", "198 2006/10", "198 2006/12", "198 2007/02",
> > "198 2007/04", "198 2007/06", "198 2007/08", "198 2007/10", "198 2007/12",
> > "198 2008/01", "202 2006/04", "202 2006/05", "202 2006/07", "202 2006/10",
> > "202 2006/12", "202 2007/02", "202 2007/04", "202 2007/06", "202 2007/08",
> > "202 2007/10", "202 2007/12", "202 2008/01", "215 2006/04", "215 2006/05",
> > "215 2006/07", "215 2006/10", "215 2006/12", "215 2007/02", "215 2007/04",
> > "215 2007/06", "215 2007/08", "215 2007/10", "215 2007/12", "215 2008/01",
> > "61 2006/10", "61 2006/12", "61 2007/02", "61 2007/04", "61 2007/06",
> > "61 2007/08", "61 2007/10", "61 2007/12", "61 2008/01", "BC 2006/04",
> > "BC 2006/05", "BC 2006/07", "BC 2006/10", "BC 2006/12", "BC 2007/02",
> > "BC 2007/04", "BC 2007/06", "BC 2007/08", "BC 2007/10", "BC 2007/12",
> > "BC 2008/01", "HC 2006/04", "HC 2006/05", "HC 2006/07", "HC 2006/10",
> > "HC 2006/12", "HC 2007/02", "HC 2007/04", "HC 2007/06", "HC 2007/08",
> > "HC 2007/10", "HC 2007/12", "HC 2008/01", "SC 2006/04", "SC 2006/05",
> > "SC 2006/07", "SC 2006/12", "SC 2007/02", "SC 2007/04", "SC 2007/06",
> > "SC 2007/08", "SC 2007/10", "SC 2007/12", "SC 2008/01")))
> >
> > #this is what I have tried
> > subset(ffg, rownames(ffg)!=rownames(ffg[apply(ffg, MARGIN=1, sum)==0,]))
> >
> > Thanks in advance
--
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
Dr. Gavin Simpson [t] +44 (0)20 7679 0522
ECRC, UCL Geography, [f] +44 (0)20 7679 0565
Pearson Building, [e] gavin.simpsonATNOSPAMucl.ac.uk
Gower Street, London [w] http://www.ucl.ac.uk/~ucfagls/
UK. WC1E 6BT. [w] http://www.freshwaters.org.uk
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
More information about the R-help
mailing list