21

I'm trying to delete a row from a data frame in which each row has a name. I cannot use indexes to delete the rows, only it's name. I have this dataframe:

DF<- data.frame('2014' = c(30,20,4, 50), '2015' = c(25,40,6, 65), row.names = c("mobile login", "computer login","errors", "total login"))

I've tried

DF["mobile login",] <- NULL

and

DF <- DF[-"mobile login",]

and more combinations with no results.

What can I do? Thanks

PS: The last row is the sum of the first two (there are other in the real DF, that's only an example), and once they are added, I don't need them, only the result, the "total login" value.

Theo Sloot
  • 287
  • 2
  • 3
  • 10

1 Answers1

26

Use %in% along with an appropriate subset of your data frame. To remove the rows named errors and mobile login you can use the following code:

row.names.remove <- c("errors", "mobile login")

> DF[!(row.names(DF) %in% row.names.remove), ]
               X2014 X2015
computer login    20    40
total login       50    65
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360