Removing display of row names from data frame

后端 未结 5 1655
遥遥无期
遥遥无期 2020-11-27 16:15

I am creating a dataframe using this code:

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

Which results in this:

相关标签:
5条回答
  • 2020-11-27 16:21

    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>
    
    0 讨论(0)
  • 2020-11-27 16:23

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

    kable(df, row.names = F)
    
    0 讨论(0)
  • 2020-11-27 16:29

    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)
    
    0 讨论(0)
  • 2020-11-27 16:34

    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 :)

    0 讨论(0)
  • 2020-11-27 16:35

    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

    0 讨论(0)
提交回复
热议问题