steppermotor
steppermotor

Reputation: 701

Removing an entry in r

In SAS there is the capability to remove an observation through a simple if statement. For instance if I wanted to delete the rows where Year = #. I could write:

If year == "#" then delete; 

Is there an equivalent way in r? I tried this:

 DF<- DF[!(DF$Year == "#"),]

Data set in R:

Year        Month
#           June
#           July 
#           August
2015        August

But when I run DF$year, I still get # as a factor level when I thought it would be filtered out?

Upvotes: 2

Views: 106

Answers (1)

Brandon Bertelsen
Brandon Bertelsen

Reputation: 44648

You're doing it right. It will still be a factor level, that doesn't mean it actually exists in the vector DF$year. You can drop the extra factor level if you need to with droplevels or by using factor again DF$year <- factor(DF$year)

If you need to convert a factor with numeric labels to a numeric vector, you can use:

as.numeric( ## convert to numeric as expected
  as.character( ## because factors are numbers with labels
    your_factor 
    )
  )
)

Upvotes: 3

Related Questions