从数据框中删除行名称的显示 [英] Removing display of row names from data frame

查看:34
本文介绍了从数据框中删除行名称的显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用此代码创建一个数据框:

I am creating a dataframe using this code:

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

结果如下:

    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

然后我尝试按照建议删除行名称(1、2、3 等)此处 使用此代码:

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

rownames(df) <- NULL

但是当我打印出 df 时,它仍然显示行名称.有没有办法在创建数据框时不包含行名称?我发现了一个关于 row.name = FALSE 的建议,但是当我尝试它时,我得到了错误(我可能把它放在了错误的地方).

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

我想要做的是将日期框转换为 HTML 表格,并且我不希望表格中出现行名.

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.

推荐答案

您已成功删除行名称.如果没有行名,print.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

您可以使用参数 row.names 作为 FALSE 来禁止在 print.data.frame 中打印行名称和数字.

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

如评论中所写,您希望将其转换为 HTML.从 xtableprint.xtable 文档中,您可以看到参数 include.rownames 会起作用.

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>

这篇关于从数据框中删除行名称的显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆