Re: [R] Remove line from data file

2022-09-19 Thread avi.e.gross
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  On Behalf Of Parkhurst, David
Sent: Sunday, September 18, 2022 8:49 AM
To: CALUM POLWART 
Cc: R-help@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 
Date: Sunday, September 18, 2022 at 7:25 AM
To: Parkhurst, David 
Cc: R-help@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, 
mailto:parkh...@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@r-project.org<mailto:R-help@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]]

__
R-help@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.


Re: [R] Remove line from data file

2022-09-19 Thread Parkhurst, David
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 
Date: Sunday, September 18, 2022 at 7:25 AM
To: Parkhurst, David 
Cc: R-help@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, 
mailto:parkh...@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@r-project.org<mailto:R-help@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]]

__
R-help@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.


Re: [R] Remove line from data file

2022-09-18 Thread CALUM POLWART
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,  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 
> *Date: *Sunday, September 18, 2022 at 7:25 AM
> *To: *Parkhurst, David 
> *Cc: *R-help@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,  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@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]]

__
R-help@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.


Re: [R] Remove line from data file

2022-09-18 Thread CALUM POLWART
>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,  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@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]]

__
R-help@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.


[R] Remove line from data file

2022-09-18 Thread Parkhurst, David
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@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.