[R] Subsetting Enigma: More rows after dataframe[-list,]?

Gavin Simpson gavin.simpson at ucl.ac.uk
Wed Jul 18 12:15:50 CEST 2007


On Wed, 2007-07-18 at 11:40 +0200, Johannes Graumann wrote:
> Hello again,
> 
> I'm trying to purge the indexes in i.delete from frame and end up with more
> rows!? Please be so kind and let me know where I screw this up ...

I think you'll have to explain why you think there are more rows after
using i.delete than before. (1975 - 173 = 1802). By purge, you mean
delete the rows indexed by i.delete? If so, you are doing nothing wrong:

> frame <- data.frame(matrix(runif(1975*10), ncol = 10))
> i.delete <- sample(nrow(frame), 173) # random rows to delete
> nrow(frame)
[1] 1975
> nrow(frame[-i.delete, ])
[1] 1802
> nrow(frame) > nrow(frame[-i.delete, ])
[1] TRUE

G

> 
> Joh
> 
> > i.delete
>   [1]   40   45  165  212  253  270  280  287  301  352  421  433  463  467 
> 487
>  [16]  517  537  542  573  594  596  612  614  621  635  650  696  699  707 
> 732
>  [31]  738  776  826  891  892  936  937  935  940  976  988  995 1037 1043
> 1059
>  [46] 1081 1111 1123 1128 1132 1140 1153 1155 1165 1176 1179 1200 1281 1289
> 1300
>  [61] 1320 1346 1356 1366 1369 1396 1406 1420 1428 1429 1471 1474 1475 1525
> 1540
>  [76] 1554 1565 1645 1667 1665 1706 1711 1724 1764 1788 1791 1805 1808 1847
> 1881
>  [91]   10   18  137  238  254  260  262  288  292  314  338  349  414  447 
> 457
> [106]  465  470  478  511  530  536  552  582  588  644  655  687  693  701 
> 724
> [121]  739  763  771  836  848  859  888  900  902  919  939  972  979  989
> 1000
> [136] 1002 1015 1020 1026 1029 1032 1055 1060 1073 1088 1104 1117 1124 1130
> 1135
> [151] 1144 1221 1225 1249 1251 1257 1376 1384 1386 1453 1487 1529 1532 1534
> 1605
> [166] 1624 1633 1646 1648 1702 1787 1948 1951
> 
> > length(i.delete)
> [1] 173
> 
> > nrow(frame)
> [1] 1975
> 
> > nrow(frame[-i.delete,])
> [1] 1802
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
-- 
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
 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