Subset problem in R

Asked

Viewed 171 times

2

I’m having trouble using subset in R. I believe it’s an easy question, but I’m not getting it right.

I want to make a subset of data that I can keep, only observations that do not have the value of the variable X.trimws.relcoop.SITUAÇÃO ... b ... is not equal to Cancelada and the value of the variable risco not equal to Não classificada.

So that’s what I came for:

dados<-data.frame(subset(dados,(X.trimws.relcoop.SITUAÇÃO...b...!="Cancelada"&risco!="Não classificada")))

I tried that too:

dados<-dados[dados$X.trimws.relcoop.SITUAÇÃO...b... != "Cancelada" & dados$risco!= "Não classificada", ]

But in both the subset of my data is being made by or instead of and.

I’m sorry to ask such an easy question, but I believe this answer will help me on the logic of R and maybe you can help others.

1 answer

2


To remove only the lines that are "Unclassified" and "Canceled", the easiest way is like this:

Consider this data.frame:

df <- data.frame(
  x = c("Cancelada", "Outro", "Outro", "Cancelada"),
  y = c("Não classificada", "Não classificada", "Outro", "Outro")
)

subset(df, !(x == "Cancelada" & y == "Não classificada"))
          x                y
2     Outro Não classificada
3     Outro            Outro
4 Cancelada            Outro

You could also change the & (and) by | (or):

subset(df, x != "Cancelada" | y != "Não classificada")
  • Very good! Silly mine. It worked. Thank you!

  • 1

    Cool, if it worked you can accept the answer, as shows the link

Browser other questions tagged

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