# [R] Deleting a specific value in a column of a data frame

David L Carlson dc@r|@on @end|ng |rom t@mu@edu
Sat Jun 23 19:25:00 CEST 2018

```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:r-help-bounces using r-project.org] On Behalf Of Bert Gunter
Sent: Saturday, June 23, 2018 9:27 AM
To: javad bayat <j.bayat194 using gmail.com>
Cc: R-help <R-help using r-project.org>
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 <j.bayat194 using gmail.com> 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
> M.Sc. Environment Engineering
> Alternative Mail: bayat194 using yahoo.com
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

[[alternative HTML version deleted]]

______________________________________________
R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help