How to Remove Rows of a Matrix by Row Name, Rather Than Numerical Index

How to remove rows of a matrix by row name, rather than numerical index?

When working with indexing, you cannot use "negative" character vectors. You can convert to logical with %in%

g[!rownames(g) %in% remove, ]  # ! is logical negation

If you really wanted to use negative-indexing this could be done:

g[-which(rownames(g) %in% remove), ] #which converts to numeric, so minus sign OK

... however it has a nasty potential erroneous result that arises when there are not any rownames in the target vector. The result may be no values returned.

Dataframe in R remove rows based on certain row names

The concept of row names in base R can have its uses; however, if you want to perform any sort of analysis with them, it's always better to make them an actual column in your data frame. Here is a replication of your data:

df <- data.frame(Age = c(27, 28, 25))
rownames(df) <- paste("Player", 1:3)
df

Age
Player 1 27
Player 2 28
Player 3 25

This is how you can make the row names an actual part of your data. I provide two methods.

Turning row names into a data column

Method 1: base R

df$Player <- rownames(df)
rownames(df) <- NULL # This code will remove the old row names and turn them into row numbers
df

Age Player
1 27 Player 1
2 28 Player 2
3 25 Player 3

Method 2: the rownames_to_column() function in the tibble package

library(tibble)

rownames_to_column(df)

rowname Age
1 Player 1 27
2 Player 2 28
3 Player 3 25

Subsetting the data frame based on the player

Now that the row names are in your data frame, you can use them to filter the data. Assuming that your data is currently:

df

rowname Age
1 Player 1 27
2 Player 2 28
3 Player 3 25

You can do it with base R:

df[!(df$Player %in% c("Player 1", "Player 2")), ]

Age Player
3 25 Player 3

Or if you prefer the dplyr syntax:

library(dplyr)

df %>%
filter(!(Player %in% c("Player 1", "Player 2")))

Age Player
1 25 Player 3

Remove a list of rownames from matrix in R

You're looking for %in%:

testMat[!rownames(testMat) %in% rem, ]

[,1] [,2] [,3]
a 1 11 21
b 2 12 22
c 3 13 23
g 7 17 27
h 8 18 28
j 10 20 30

Negative indexing works for numeric indices only.

Removing display of row names from data frame

You have successfully removed the row names. The print.data.frame method just shows the row numbers if no row names are present.

df1 <- data.frame(values = rnorm(3), group = letters[1:3],
row.names = paste0("RowName", 1:3))
print(df1)
# values group
#RowName1 -1.469809 a
#RowName2 -1.164943 b
#RowName3 0.899430 c

rownames(df1) <- NULL
print(df1)
# values group
#1 -1.469809 a
#2 -1.164943 b
#3 0.899430 c

You can suppress printing the row names and numbers in print.data.frame with the argument row.names as FALSE.

print(df1, row.names = FALSE)
# values group
# -1.4345829 d
# 0.2182768 e
# -0.2855440 f

Edit: As written in the comments, you want to convert this to HTML. From the xtable and print.xtable documentation, you can see that the argument include.rownames will do the trick.

library("xtable")
print(xtable(df1), type="html", include.rownames = FALSE)
#<!-- html table generated in R 3.1.0 by xtable 1.7-3 package -->
#<!-- Thu Jun 26 12:50:17 2014 -->
#<TABLE border=1>
#<TR> <TH> values </TH> <TH> group </TH> </TR>
#<TR> <TD align="right"> -0.34 </TD> <TD> a </TD> </TR>
#<TR> <TD align="right"> -1.04 </TD> <TD> b </TD> </TR>
#<TR> <TD align="right"> -0.48 </TD> <TD> c </TD> </TR>
#</TABLE>

How to delete specific rows and columns from a matrix in a smarter way?

You can do:

t1<- t1[-4:-6,-7:-9]

How can I delete the rownames which contain specific text in R?

We can use grep with -

df[-grep('Apple', row.names(df)),, drop = FALSE]

Or invert = TRUE

df[grep('Apple', row.names(df), invert = TRUE),, drop = FALSE]

With data.frame, the rownames and column names attributes cannot be empty. An option is to convert it to numeric index

i1 <- grep('Apple', row.names(df))
row.names(df)[i1] <- seq_along(i1)

Or convert to a matrix and then change those row names to blank ("")

m1 <- as.matrix(df)
row.names(m1)[i1] <- ""

as matrix allows duplicated rownames while data.frame doesn't. It is also possible to completely remove the rowname attribute, but it has to be across the whole object

How do I retrieve a matrix column and row name by a matrix index value?

First you need to get the row and column of that index using arrayInd.

k <- arrayInd(4, dim(mdat))

You can then get the right name by getting that element of the row and column names

rownames(mdat)[k[,1]]
colnames(mdat)[k[,2]]

Or both at once using mapply:

mapply(`[[`, dimnames(mdat), k)

Delete rows containing specific strings in R

This should do the trick:

df[- grep("REVERSE", df$Name),]

Or a safer version would be:

df[!grepl("REVERSE", df$Name),]


Related Topics



Leave a reply



Submit