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 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
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 at 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 at 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
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.000You 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