[R] Data frame search and remove questions

David Winsemius dwinsemius at comcast.net
Fri Oct 16 03:15:22 CEST 2009


On Oct 15, 2009, at 1:17 PM, Douglas M. Hultstrand wrote:

> Hello,
>
> I have a couple questions about removing rows from a data frame and  
> creating a new data frame with the removed values.  I provided an  
> example data frame (d) below.
>
> Questions:
> 1) How can I search for "-999.000" and remove the entire row from  
> data frame "d"? (all -999 values will be in sd_diff)
> 2) Can I create a new data frame "d.new" that only contains the  
> removed rows?
> 3) How can I remove the last two rows from a data frame.  (I used  
> append command to add two values to the end of the data)
>
> > d
>  lat.add lon.add PPT.add Z.add sd_dif
> 1    37.67  -95.48   1.000   368  1.017
> 2    38.82  -92.22  13.208   383  5.737
> 3    37.30  -95.50   6.096   130  4.377
> 4    37.08  -95.57   0.508   106  -999.000
> 5    38.73  -93.55   6.350   370  6.233
> 6    38.83  -94.88   0.254     5  8.607
> 7    38.33  -96.18   0.508    43  8.665
> 8    38.85  -94.73   1.000     5  -999.000
> 9    38.71  -93.16   1.016   320  3.717
> 10   38.95  -95.67   1.000     5  8.553
>
> > d.new
>  lat.add lon.add PPT.add Z.add sd_dif
> 1    37.08  -95.57   0.508   106  -999.000
> 2    38.85  -94.73   1.000     5  -999.000

You asked about "-999.000" which would be a character value, but you  
displayed what appears to be a numeric column since there were no  
quotes. That is the reason we ask for executable examples which yours  
is not. You could have resolved this ambiguity if you have some  
aversion to using dput(d) by at least offering the output of str(d).

At any rate, if it is a numeric column this should work even if the  
-999's are not in sd_dif.

 > d[apply(d , 1, function(x) -999 %in% x), ]
   lat.add lon.add PPT.add Z.add sd_dif
4   37.08  -95.57   0.508   106   -999
8   38.85  -94.73   1.000     5   -999

Or if they are for certain in sd_dif, then:

d[d$sd_dif==-999, ]

>
>
> Thanks for all the help,
> Doug
>
> -- 
> ---------------------------------
> Douglas M. Hultstrand, MS
> Senior Hydrometeorologist
> Metstat, Inc. Windsor, Colorado
> voice: 970.686.1253
> email: dmhultst at metstat.com
> web: http://www.metstat.com
>
> ______________________________________________
> R-help at r-project.org 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.

David Winsemius, MD
Heritage Laboratories
West Hartford, CT




More information about the R-help mailing list