[R] Remove line from data file

CALUM POLWART po|c1410 @end|ng |rom gm@||@com
Sun Sep 18 14:55:23 CEST 2022


If you want to delete row 18 you can do

mydf <- mydf[-18,]

This selects all rows other than row 18, and all columns and 'saves' it
back to the original data frame. Many people prefer to allocate to a new
dataframe so that if the -18 is wrong they can simply fix things.

I wasn't sure if you knew the row you wanted to delete. If you had a frame
with names and ages in. David Parkhurst might be row 18 and you might want
to delete him. But if the data is ever updated, he may move row.  If you
always want to delete David, it would be better to do:

mydf2 <- mydf[name != "David Parkhurst", ]




On Sun, 18 Sep 2022, 13:48 Parkhurst, David, <parkhurs using indiana.edu> wrote:

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