How to delete rows from a Data Frame in R based on the values of one of the columns?

Asked

Viewed 1,149 times

1

I want to exclude rows that have a certain value in a column.

let’s assume I have a DF where the first column is an index with alphabet letters, but, I don’t know the position, I want the lines with vowel Indice to be removed as I do.

  • 1

    Welcome to Stackoverflow Brasil! Unfortunately, this question cannot be reproduced by anyone trying to answer it. Please, take a look at this link and see how to ask a reproducible question in R. So, people who wish to help you will be able to do this in the best possible way.

2 answers

5

To search for alphanumeric patterns, it is best to use the grep or grepl.

set.seed(6323)    # Torna os resultados reprodutíveis

n <- 100
DF <- data.frame(A = sample(LETTERS, n, TRUE), X = rnorm(n))

inx <- grepl("[AEIOU]", toupper(DF[[1]]))

DF2 <- DF[!inx, ]        # Usando um indíce lógico
DF3 <- subset(DF, !inx)  # Usando avaliação não-standard

identical(DF2, DF3)
#[1] TRUE

head(DF2)
#  A           X
#2 F -0.54113708
#3 D -0.72646708
#4 V  0.02213349
#6 X -0.64141533
#7 F -1.06416864
#8 Y -0.90681239

Another equivalent way will be with %in%.

inx2 <- toupper(DF[[1]]) %in% c("A", "E", "I", "O", "U")
DF4 <- DF[!inx2, ]        # Usando um indíce lógico mais uma vez

identical(DF2, DF4)
#[1] TRUE

1


I understood later that I could just make a reallocation on the variable that contained my Data Frame forcing it to obey the conditions that I desired.

In this case the "profile" is a variable with the value I want to redeem.

df <- df[df$Perfil.do.entrevistado==perfil,]

Thus it assigns all that the line had the desired value and the others are automatically discarded.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.