[R] removing rows from a dataframe

Douglas Bates bates at stat.wisc.edu
Mon Sep 4 00:31:53 CEST 2000


Peter Dalgaard BSA <p.dalgaard at biostat.ku.dk> writes:

> "Jeff Miller" <jdm at xnet.com> writes:
> 
> >     Hi,
> > 
> >     I have a dataframe, hilodata, which looks like this:
> > 
> > > hilodata
> >     sym         date               maxprice           minprice         ntick
> >  1  ABK     19910711     11.1867461      0.0000000       108
> >  2  ABK     19910712     11.5298979     11.1867461      111
> >  3  ABK     19910715     11.7357889     11.4612675       52
> >  4  ABK     19910716     11.5298979     11.3240068       51
> >  5  ABK     19910717     11.4612675     11.1181158       23
> >  6 CSCO   19910102      0.1553819      0.0000000         106
> >  7 CSCO   19910103      0.1527778      0.1458333         166
> >  8 CSCO   19910104      0.1475694      0.1397569         205
> >  9 CSCO   19910107      0.1414931      0.1362847         164
> > 10 CSCO  19910108      0.1440972      0.1380208         127
> > 11 CSCO  19910109      0.1467014      0.1414931          83
> > 12 CSCO  19910110      0.1440972      0.1414931          70
> > 13  KMP   19991213     40.5625000      0.0000000         63
> > 14  KMP   19991214     41.3125000     40.4375000        71
> > 15  KMP   19991215     42.1875000     41.1875000        99
> > 16  KMP   19991216     43.5000000     42.1250000        96
> > 17  KMP   19991217     43.8750000     42.5625000        77
> > 18  KMP   19991220     44.0000000     43.3750000        56
> > 
> >     I'd like to make a new dataframe, newhilodata, that gets rid of each
> > symbols first
> >     row. So, in this example,  I'd like to get rid of rows 1, 6 and 13,
> > leaving
> <snip>
> >     what is the best way to do this in R?
> 
> Well 
> 
> newhilodata <- hilodata[-c(1,6,13),]
> 
> should do it. To solve the more general problem of omitting the first
> in each group, assuming "sym" is a factor, try something like
> 
> newhilodata <- subset(hilodata, diff(c(0,as.integer(sym))) != 0)
> 
> (actually, the as.integer is unnecessary because the c() will unclass
> the factor automagically)

Alternatively, you could use the match function because it returns the
first match.

 > jm
     sym     date   maxprice   minprice ntick
 1   ABK 19910711 11.1867461  0.0000000   108
 2   ABK 19910712 11.5298979 11.1867461   111
 3   ABK 19910715 11.7357889 11.4612675    52
 4   ABK 19910716 11.5298979 11.3240068    51
 5   ABK 19910717 11.4612675 11.1181158    23
 6  CSCO 19910102  0.1553819  0.0000000   106
 7  CSCO 19910103  0.1527778  0.1458333   166
 8  CSCO 19910104  0.1475694  0.1397569   205
 9  CSCO 19910107  0.1414931  0.1362847   164
 10 CSCO 19910108  0.1440972  0.1380208   127
 11 CSCO 19910109  0.1467014  0.1414931    83
 12 CSCO 19910110  0.1440972  0.1414931    70
 13  KMP 19991213 40.5625000  0.0000000    63
 14  KMP 19991214 41.3125000 40.4375000    71
 15  KMP 19991215 42.1875000 41.1875000    99
 16  KMP 19991216 43.5000000 42.1250000    96
 17  KMP 19991217 43.8750000 42.5625000    77
 18  KMP 19991220 44.0000000 43.3750000    56
 > match(unique(jm$sym), jm$sym)
 [1]  1  6 13
 > jm <- jm[ -match(unique(jm$sym), jm$sym), ]
 > jm
     sym     date   maxprice   minprice ntick
 2   ABK 19910712 11.5298979 11.1867461   111
 3   ABK 19910715 11.7357889 11.4612675    52
 4   ABK 19910716 11.5298979 11.3240068    51
 5   ABK 19910717 11.4612675 11.1181158    23
 7  CSCO 19910103  0.1527778  0.1458333   166
 8  CSCO 19910104  0.1475694  0.1397569   205
 9  CSCO 19910107  0.1414931  0.1362847   164
 10 CSCO 19910108  0.1440972  0.1380208   127
 11 CSCO 19910109  0.1467014  0.1414931    83
 12 CSCO 19910110  0.1440972  0.1414931    70
 14  KMP 19991214 41.3125000 40.4375000    71
 15  KMP 19991215 42.1875000 41.1875000    99
 16  KMP 19991216 43.5000000 42.1250000    96
 17  KMP 19991217 43.8750000 42.5625000    77
 18  KMP 19991220 44.0000000 43.3750000    56
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list