[R] Remove line from data file

@vi@e@gross m@iii@g oii gm@ii@com @vi@e@gross m@iii@g oii gm@ii@com
Mon Sep 19 18:18:07 CEST 2022


David,

As others have said, there are many possible answers for a vague enough question.

For one-time data it is often easiest to simply change the data source as you say you did in EXCEL.

Deleting the 18th row can easily be done in R and might make sense if you get daily data and decided the 18th reporting station is not reliable and should always be excluded. As has been shown, the usual paradigm in R is to filter the data through a set of conditions and a very simple one is to specify which indices in rows and/or columns to exclude.

If you already have your data in mydata.old, then you can make a mydata.new that excludes that 18th row with something as simple as:

mydata.new <- mydata.old[ -18, ]

Since your question was not focused, the answer mentioned that it is common to delete based on all kinds of conditions. An example would be if you did not want to remove row 18 specifically but any row where a column says the collector/reporter of the info was "Smith" which may remove many rows in the data, or you wanted only data with a column giving a date in 2021 and not before or after.

This filter method is not a deletion per se, but a selective retention, and often has the same result. If your goal includes making the deletion of selected data permanent, of course, it is wise then to save the altered data in another file so later use starts with what you want. 

Actually removing a row from an original data.frame is not something people normally do. A data.frame is a list of vectors of some length and generally does not allow operations that might produce vectors of unequal length. You can set the entire row to be filled with NA if you want but removing the actual row in-place is the kind of thing  I do not normally see. In a sense, R is wasteful that way as you often end up making near-copies of your data and sometimes simply re-assigning the result to the same variable and expecting the old version to be garbage collected. As I said, the paradigm is selection more than alteration/deletion.

It is, of course, possible to create your own data structures where you could do something closer to a deletion of a row while leaving the rest in place but there likely is no need for your small amount of data. 

Note columns in R can be deleted easily because they are a top level entry in a LIST. mydata$colname <- NULL or similar variants will remove a column cleanly in the original data.frame. But as noted, rows in R do not really exist other than as a construct that tries to bind the nth elements of each underlying vector representing the columns. 

Of course we now can have list-columns in things like tibbles which makes me wonder ... 




-----Original Message-----
From: R-help <r-help-bounces using r-project.org> On Behalf Of Parkhurst, David
Sent: Sunday, September 18, 2022 8:49 AM
To: CALUM POLWART <polc1410 using gmail.com>
Cc: R-help using r-project.org
Subject: Re: [R] Remove line from data file

Thank you for your reply.  I meant from the dataframe, but that s one of the terms I had forgotten.  I created that from read.csv, the csv file coming from Excel.  Last night I went ahead and made the change(s) using Excel.

For future reference, when I look at your solutions below, what do you mean by  value to delete ?  Could that just be a row number?  I was wanting to delete something like the 18th row in the dataframe?

From: CALUM POLWART <polc1410 using gmail.com>
Date: Sunday, September 18, 2022 at 7:25 AM
To: Parkhurst, David <parkhurs using indiana.edu>
Cc: R-help using r-project.org <R-help using r-project.org>
Subject: Re: [R] Remove line from data file From the file? Or the data frame once its loaded?

What format is the file? CSV?

Do you know the line that needs deleted?

mydf <- read.csv("myfile.csv")

mydf2 <- mydf[-columnName == "valuetodelete", ] # Note the - infront of column name # or perhaps columnName != "value to delete", ]

write.csv(mydf2, "mydeletedfile.csv")




On Sun, 18 Sep 2022, 10:33 Parkhurst, David, <parkhurs using indiana.edu<mailto:parkhurs using indiana.edu>> wrote:
I ve been retired since  06 and have forgotten most of R.  Now I have a use for it.  I ve created a data file and need to delete one row from it.  How do I do that?

DFP (iPad)
______________________________________________
R-help using r-project.org<mailto:R-help using r-project.org> mailing list -- To UNSUBSCRIBE and more, see 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.

	[[alternative HTML version deleted]]



More information about the R-help mailing list