109

I am creating a dataframe using this code:

df <- data.frame(dbGetQuery(con, paste('select * from test')))

Which results in this:

    UID      BuildingCode   AccessTime
1   123456   BUILD-1        2014-06-16 07:00:00
2   364952   BUILD-2        2014-06-15 08:00:00
3    95865   BUILD-1        2014-06-06 09:50:00

I am then trying to remove the row names (1, 2, 3, etc) as suggested here by using this code:

rownames(df) <- NULL

But then when I print out df it still displays the row names. Is there a way to not include the row names when creating the data frame? I found a suggestion about row.name = FALSE but when I tried it I just got errors (I might have placed it in the wrong place).

EDIT: What I want to do is convert the dateframe to a HTML table and I don't want the row name to be present in the table.

5
  • 1
    It is not clear what you mean by "when I print out df". Is row.names = FALSE in write.table relevant? Commented Jun 26, 2014 at 10:40
  • @Henrik probably offers the best solution here. I wonder though why doing something like attributes(df)$row.names <- NULL erases all values form df Commented Jun 26, 2014 at 10:43
  • What I mean by "when I print out df" is I type df into the terminal which then prints out the data frame. I don't have write.table because I'm not writing a table (and when I do I'm going to be using xtable). Commented Jun 26, 2014 at 10:44
  • 9
    so just print(df, row.names = F) should do Commented Jun 26, 2014 at 10:45
  • @DavidArenburg your correct, that works! But how would I put that into a html table? Commented Jun 26, 2014 at 10:50

10 Answers 10

132

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>
Sign up to request clarification or add additional context in comments.

5 Comments

After doing as I said above, when I do attributes(df) I get this result: $row.names [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15. Does this not indicate that the row names are still there? Basically what I want to do is convert the DF to a HTML table and I just don't want the row name to be present.
@DomAbbott From the R docs, a data.frame is "is a list of variables of the same number of rows with unique row names, given class 'data.frame'. If no variables are included, the row names determine the number of rows." Hence row names are always present in the attributes. How do you do your conversion?
What do you mean by conversion? I am using this code to create a html table print(xtable(df), type="html", file="example.html") but this includes the row names.
@DomAbbott add this comment to your post, so we know what you are trying to accomplish.
@DomAbbott I've edited so you get what you want. You need to use the include.rownames argument.
40

Yes I know it is over half a year later and a tad late, BUT

row.names(df) <- NULL

does work. For me at least :-)

And if you have important information in row.names like dates for example, what I do is just :

df$Dates <- as.Date(row.names(df))

This will add a new column on the end but if you want it at the beginning of your data frame

df <- df[,c(7,1,2,3,4,5,6,...)]

Hope this helps those from Google :)

Comments

17

If you want to format your table via kable, you can use row.names = F

kable(df, row.names = F)

Comments

14

A dplyr solution :

df = df %>% `rownames<-`( NULL )

2 Comments

This isn't a "dplyr solution". If anything it's a magrittr solution. But a much simpler solution would be to just call rownames(df) <- NULL.
See stackoverflow.com/a/72318941/8806649 for tidyverse solution
8

...or simply:

library(tidyverse)

df_data_with_rownames %>%
SOME STUFF %>%
tibble::remove_rownames() -> wrangled_df_data_without_rownames

Comments

2

Recently I had the same problem when using htmlTable() (‘htmlTable’ package) and I found a simpler solution: convert the data frame to a matrix with as.matrix():

htmlTable(as.matrix(df))

And be sure that the rownames are just indices. as.matrix() conservs the same columnames. That's it.

UPDATE

Following the comment of @DMR, I did't notice that htmlTable() has the parameter rnames = FALSE for cases like this. So a better answer would be:

htmlTable(df, rnames = FALSE)

1 Comment

htmlTable has the rnames parameter for this: htmlTable(df, rnames = FALSE)
1

My answer is intended for comment though but since i havent got enough reputation, i think it will still be relevant as an answer and help some one.

I find datatable in library DT robust to handle rownames, and columnames

Library DT
datatable(df, rownames = FALSE)  # no row names 

refer to https://rstudio.github.io/DT/ for usage scenarios

Comments

0

I just wanted to add that data.table has an option for removing rownames when printing data.table's.

options(datatable.print.rownames = F)

This solved my problem in the console and when rendering RMarkdown files. See other printing options here

Comments

-1

Another simple way is assigning c() to your rownames to get rid of your row names like this (thanks @Anders for data):

df <- data.frame(values = rnorm(3), group = letters[1:3],
                  row.names = paste0("RowName", 1:3))
df
#>             values group
#> RowName1 0.4189236     a
#> RowName2 1.8604397     b
#> RowName3 0.7030387     c
rownames(df) <- c()
print(df)
#>      values group
#> 1 0.4189236     a
#> 2 1.8604397     b
#> 3 0.7030387     c

Created on 2022-07-09 by the reprex package (v2.0.1)

Comments

-1

simply try rownames(df) <- ""

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.