[R] Data frame search and remove questions

Ista Zahn istazahn at gmail.com
Fri Oct 16 03:09:54 CEST 2009


Hi Doug,
This is all pretty basic indexing stuff, maybe
http://cran.r-project.org/doc/contrib/Short-refcard.pdf will get you
started. Although... See below.


http://www.mail-archive.com/r-help@r-project.org/msg70906.html


On Thu, Oct 15, 2009 at 1:17 PM, Douglas M. Hultstrand
<dmhultst at metstat.com> 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)

Usually I do something like

d <- d[-which(d$sd_dif == -999.000),]

but the discussion prompted by
http://www.mail-archive.com/r-help@r-project.org/msg70906.html has
made me a little nervous about this. Seems to work most of the time
though, and I can't figure out how to do it with all.equal.

> 2) Can I create a new data frame "d.new" that only contains the removed
> rows?

d.new <- d[which(d$sd_dif == -999.000),]

will do the trick, with the same possible issues linked to above. Also
make sure you do this before the previous step, otherwise d$sd_dif
will not have any values equal to -999.000!

> 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 <- d[ 1:(dim(d)[1]-2) , ]


-Ista

>
>> 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
>
>
> 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.
>



-- 
Ista Zahn
Graduate student
University of Rochester
Department of Clinical and Social Psychology
http://yourpsyche.org




More information about the R-help mailing list