Removing display of row names from data frame

后端 未结 5 1682
遥遥无期
遥遥无期 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)
    #
    #
    #
    #
    #
    #
    #
    #
    values group
    -0.34 a
    -1.04 b
    -0.48 c

提交回复
热议问题