Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete a row in a data frame by name in R

Tags:

dataframe

r

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.

like image 437
Theo Sloot Avatar asked May 30 '16 12:05

Theo Sloot


People also ask

How do I remove a row from my name?

In general, the rows are removed by using the row index number but we can do the same by using row names as well. This can be done by storing the row names that should be removed in a vector and then removing through subsetting with single square brackets as shown in the below examples.

How do I delete a row in a data frame?

You can use the drop function to delete rows and columns in a Pandas DataFrame.

How do I delete a row in R studio?

You cannot actually delete a row, but you can access a data frame without some rows specified by negative index. This process is also called subsetting in R language. A Big Note: You should provide a comma after the negative index vector -c().


1 Answers

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
like image 155
Tim Biegeleisen Avatar answered Sep 20 '22 20:09

Tim Biegeleisen