1

Possible Duplicate:
How to delete a row in R

I can't figure out how to simply remove row (n) from a dataframe in R. R's documentation and intro manual are so horribly written, they are virtually zero help on this very simple problem. Also, every explanation i've found here/ on google is for removing rows that contain strings, or duplicates, etc, which have been excessively advanced for my problem and lead me to introduce more bugs and get nowhere. I just want to remove a row.

Thanks in advance for your help.

fyi the list is in the variable eld, which has 5 columns and 33 rows. I would like to remove row 14. I initialized eld with the following command

eld <- read.table("election2012.txt")

so my desired result is

eldNew <- eld(minus row 14)
Community
  • 1
  • 1
Info5ek
  • 1,227
  • 4
  • 17
  • 25
  • 9
    if you want to attract fewer downvotes, less complaining about R's documentation would probably help. (You are of course entitled to your opinion, and you may even be right, but questions phrased as "I've tried to read the documentation but just haven't been able to find the answer" are viewed more charitably than those phrased as "the documentation sucks" ...) – Ben Bolker Nov 22 '12 at 22:12

1 Answers1

23
eldNew <- eld[-14,]

See ?"[" for a start ...

For ‘[’-indexing only: ‘i’, ‘j’, ‘...’ can be logical vectors, indicating elements/slices to select. Such vectors are recycled if necessary to match the corresponding extent. ‘i’, ‘j’, ‘...’ can also be negative integers, indicating elements/slices to leave out of the selection.

(emphasis added)

edit: looking around I notice How to delete the first row of a dataframe in R? , which has the answer ... seems like the title should have popped to your attention if you were looking for answers on SO?

edit 2: I also found How do I delete rows in a data frame? , searching SO for delete row data frame ...

Also http://rwiki.sciviews.org/doku.php?id=tips:data-frames:remove_rows_data_frame

Community
  • 1
  • 1
Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
  • 3
    Thanks. Your extract from the R documentation shows what I mean about how bad the writing is, it makes basically no sense to someone that hasn't already mastered the language, in my opinion. So thanks for the code snippet, worked like a charm. – Info5ek Nov 22 '12 at 22:15
  • Thanks for asking, no I didn't find that actual result and was instead getting answers to much more complex problems. That is more a result of SO's search algorithm than me not doing research. I was looking around for 30 mins but searched "deleting a row from a data frame in R", which explains why I didn't found that page in my results, as I thought being more specific would help. I have received negative points for asking this which I find sort of silly. at any rate, This question can be removed since it's essentially a duplicate. – Info5ek Nov 22 '12 at 22:20
  • 2
    There is a long conversation (which some of us have had many times) to be had about R's documentation. The online help really is (as you suggest) a *technical* manual. There are literally hundreds of introductions to R around the web, which are intended as replacements/supplements for the built-in docs. For example you might browse http://rwiki.sciviews.org/doku.php?id=tips:data-frames ... There are also non-free documents like http://www.amazon.com/R-Dummies-Joris-Meys/dp/1119962846 (see p. 108 for the answer to your question ...) – Ben Bolker Nov 22 '12 at 22:30
  • 2
    @user1800340 It helps to look at the examples in the help. In fact the following line is present: `x <- x[-1] # delete the 1st element of x1` – sebastian-c Nov 22 '12 at 23:34
  • 1
    It's easy to direct newcomers to the help file, but keep in mind that the help file presupposes a familiarity with R that a newcomer just doesn't have. I try to look at it but it usually gets me nowhere as the documentation is too confusing. At any rate thanks for the suggestion. – Info5ek Nov 23 '12 at 01:15
  • 1
    @user1800340 Keep reading it in general. I know it doesn't make sense now, but it will. It will start making sense in pieces. First the function arguments, then the descriptions of the arguments, the notes. Keep reading and salvage from it what you can. – sebastian-c Nov 23 '12 at 05:08