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

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

问题描述

我正在使用以下代码创建数据框:

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

然后我尝试按照建议的

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

您可以禁止在 print.data.frame ,参数为 row.names FALSE

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。从 xtable print.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天全站免登陆