Removing Display of Row Names from Data Frame

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>

arrange delete the data frame row names

Dplyr does't support row.names you may want to use tibble::rownames_to_column()

Example

mtcars %>% 
tibble::rownames_to_column()

In your case this should work

df_tablaCruzada<-df_tablaCruzada%>%
tibble::rownames_to_column() %>%
arrange(desc(Total)) %>%
mutate(Ranking=1:nrow(df_tablaCruzada))

you can also use the add_row function of dplyr to replace the mutate in this case

Remove row names for a list of data frames using map

Because you are already in the tidyverse, one possibility is to use tibble::remove_rownames() like so:

library(tidyverse)

set.seed(42)

dat <- list(tbl1 = data.frame(x = runif(3), row.names = LETTERS[1:3]),
tbl2 = data.frame(x = runif(3), row.names = LETTERS[1:3]))
dat

# $tbl1
# x
# A 0.9148060
# B 0.9370754
# C 0.2861395
#
# $tbl2
# x
# A 0.8304476
# B 0.6417455
# C 0.5190959

dat %>%
map(~ remove_rownames(.)) -> dat
dat

# $tbl1
# x
# 1 0.9148060
# 2 0.9370754
# 3 0.2861395
#
# $tbl2
# x
# 1 0.8304476
# 2 0.6417455
# 3 0.5190959

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

Removing rows causes row.names column to appear when displayed with View()

Although it seems as such, you are not actually adding a column to the data. What you are seeing is just a result of using View(). The function is showing the "row.names" attribute of the data frame as the first column, but you didn't really add the column.

This is expected and documented behavior. From the Details section of help(View)

If there are row names on the data frame that are not 1:nrow, they are displayed in a separate first column called row.names.

So since you subsetted the data, the row names are technically not 1:nrow any more and hence the new column is introduced in the viewer.

Print your data in the console and you'll see the difference.

View(mtcars) ## because the mtcars row names are not 1:nrow

versus

mtcars

Basically, don't trust View() to display an exact representation of the actual data. Instead use attributes(), *names(), dim(), length(), etc. or just peek at the data with head().

How to remove a row in r based on the row name

We can create a logical vector by making use of the comparison operator with row.names and use that row index to subset the rows. If df1 is the data.frame object name, then do

df1[row.names(df1) != "Bacteria", , drop = FALSE]

Remove row names using capture.output in R?

To remove the row names, use row.names = FALSE directly within the print statement:

capture.output(print(aa, print.gap=3, row.names = FALSE), file="capture.txt")

Output of capture.txt:

     C1     C2     C3      C4
1000 110 10 1
0 2000 20 2
30 300 3000 30000


Related Topics



Leave a reply



Submit