Deleting a specific value in a column of a data frame

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Deleting a specific value in a column of a data frame

javad bayat
Dear R users;
I have two columns data frame (column names is A and B). I want to write a
function in order to compare column of B with A and if the values of B are
equal or greater than that of A, replace them with "NA" or delete them and
if the values of B are less than values in A, do nothing.

Sincerely.

--
Best Regards
Javad Bayat
M.Sc. Environment Engineering
Alternative Mail: [hidden email]

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Deleting a specific value in a column of a data frame

Bert Gunter-2
You understand, of course, that all columns in a data frame must be of the
same length; and that "NA" is not the same as NA?

This is pretty basic stuff and suggests you need to spend some time with an
R tutorial or two.

In any case, a construct of the form:

B[B >= A] <- NA

should do.

Cheers,
Bert



Bert Gunter

"The trouble with having an open mind is that people keep coming along and
sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )

On Sat, Jun 23, 2018 at 4:09 AM, javad bayat <[hidden email]> wrote:

> Dear R users;
> I have two columns data frame (column names is A and B). I want to write a
> function in order to compare column of B with A and if the values of B are
> equal or greater than that of A, replace them with "NA" or delete them and
> if the values of B are less than values in A, do nothing.
>
> Sincerely.
>
> --
> Best Regards
> Javad Bayat
> M.Sc. Environment Engineering
> Alternative Mail: [hidden email]
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] 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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Deleting a specific value in a column of a data frame

Rui Barradas
In reply to this post by javad bayat
Hello,
Also possible is

is.na (B) <- B >= A

Hope this helps,
Rui Barradas 


Enviado a partir do meu smartphone Samsung Galaxy.-------- Mensagem original --------De: Bert Gunter <[hidden email]> Data: 23/06/2018  15:26  (GMT+00:00) Para: javad bayat <[hidden email]> Cc: R-help <[hidden email]> Assunto: Re: [R] Deleting a specific value in a column of a data frame
You understand, of course, that all columns in a data frame must be of the
same length; and that "NA" is not the same as NA?

This is pretty basic stuff and suggests you need to spend some time with an
R tutorial or two.

In any case, a construct of the form:

B[B >= A] <- NA

should do.

Cheers,
Bert



Bert Gunter

"The trouble with having an open mind is that people keep coming along and
sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )

On Sat, Jun 23, 2018 at 4:09 AM, javad bayat <[hidden email]> wrote:

> Dear R users;
> I have two columns data frame (column names is A and B). I want to write a
> function in order to compare column of B with A and if the values of B are
> equal or greater than that of A, replace them with "NA" or delete them and
> if the values of B are less than values in A, do nothing.
>
> Sincerely.
>
> --
> Best Regards
> Javad Bayat
> M.Sc. Environment Engineering
> Alternative Mail: [hidden email]
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] 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]]

______________________________________________
[hidden email] 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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Deleting a specific value in a column of a data frame

David Carlson
In reply to this post by Bert Gunter-2
Bert's solution works if A and B are vectors, but not if they are columns in a data frame. First, let's make up some data:

set.seed(42)
Dat <- data.frame(A=runif(10), B=runif(1))
Dat
#            A         B
# 1  0.9148060 0.4577418
# 2  0.9370754 0.7191123
# 3  0.2861395 0.9346722
# 4  0.8304476 0.2554288
# 5  0.6417455 0.4622928
# 6  0.5190959 0.9400145
# 7  0.7365883 0.9782264
# 8  0.1346666 0.1174874
# 9  0.6569923 0.4749971
# 10 0.7050648 0.5603327

It is safer to preserve the original data frame and store the modified data into a new one. To insert missing values:

Dat1 <- Dat
Dat1[Dat$B >= Dat$A, ] <- NA
Dat1
#            A         B
# 1  0.9148060 0.4577418
# 2  0.9370754 0.7191123
# 3         NA        NA
# 4  0.8304476 0.2554288
# 5  0.6417455 0.4622928
# 6         NA        NA
# 7         NA        NA
# 8  0.1346666 0.1174874
# 9  0.6569923 0.4749971
# 10 0.7050648 0.5603327

Dat1 has the same number of rows as Dat with missing values for the rows that meet your logical expression. Here are three ways to remove those rows. We can delete the missing values:

Dat2 <- na.omit(Dat1)
Dat2
#           A         B
# 1  0.9148060 0.4577418
# 2  0.9370754 0.7191123
# 4  0.8304476 0.2554288
# 5  0.6417455 0.4622928
# 8  0.1346666 0.1174874
# 9  0.6569923 0.4749971
# 10 0.7050648 0.5603327

Or we can extract the rows we want by flipping the logical expression:

# Dat2 <- Dat
# Dat2 <- Dat[Dat$B < Dat$A, ]
# Dat2
#            A         B
# 1  0.9148060 0.4577418
# 2  0.9370754 0.7191123
# 4  0.8304476 0.2554288
# 5  0.6417455 0.4622928
# 8  0.1346666 0.1174874
# 9  0.6569923 0.4749971
# 10 0.7050648 0.5603327

Or we can subset the original data frame with the subset() function:

Dat2 <- Dat
Dat2 <- subset(Dat, B < A)
Dat2
#            A         B
# 1  0.9148060 0.4577418
# 2  0.9370754 0.7191123
# 4  0.8304476 0.2554288
# 5  0.6417455 0.4622928
# 8  0.1346666 0.1174874
# 9  0.6569923 0.4749971
# 10 0.7050648 0.5603327

Notice that whichever one we use, the row numbers match the original data frame.


David L. Carlson
Department of Anthropology
Texas A&M University

-----Original Message-----
From: R-help [mailto:[hidden email]] On Behalf Of Bert Gunter
Sent: Saturday, June 23, 2018 9:27 AM
To: javad bayat <[hidden email]>
Cc: R-help <[hidden email]>
Subject: Re: [R] Deleting a specific value in a column of a data frame

You understand, of course, that all columns in a data frame must be of the
same length; and that "NA" is not the same as NA?

This is pretty basic stuff and suggests you need to spend some time with an
R tutorial or two.

In any case, a construct of the form:

B[B >= A] <- NA

should do.

Cheers,
Bert



Bert Gunter

"The trouble with having an open mind is that people keep coming along and
sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )

On Sat, Jun 23, 2018 at 4:09 AM, javad bayat <[hidden email]> wrote:

> Dear R users;
> I have two columns data frame (column names is A and B). I want to write a
> function in order to compare column of B with A and if the values of B are
> equal or greater than that of A, replace them with "NA" or delete them and
> if the values of B are less than values in A, do nothing.
>
> Sincerely.
>
> --
> Best Regards
> Javad Bayat
> M.Sc. Environment Engineering
> Alternative Mail: [hidden email]
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] 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]]

______________________________________________
[hidden email] 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.

______________________________________________
[hidden email] 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.