[r] Removing display of row names from data frame

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.

This question is related to r printing dataframe output-formatting rowname

The answer is


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)

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

kable(df, row.names = F)

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


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


Examples related to r

How to get AIC from Conway–Maxwell-Poisson regression via COM-poisson package in R? R : how to simply repeat a command? session not created: This version of ChromeDriver only supports Chrome version 74 error with ChromeDriver Chrome using Selenium How to show code but hide output in RMarkdown? remove kernel on jupyter notebook Function to calculate R2 (R-squared) in R Center Plot title in ggplot2 R ggplot2: stat_count() must not be used with a y aesthetic error in Bar graph R multiple conditions in if statement What does "The following object is masked from 'package:xxx'" mean?

Examples related to printing

How do I print colored output with Python 3? Print a div content using Jquery Python 3 print without parenthesis How to find integer array size in java Differences Between vbLf, vbCrLf & vbCr Constants Printing variables in Python 3.4 Show DataFrame as table in iPython Notebook Removing display of row names from data frame Javascript window.print() in chrome, closing new window or tab instead of cancelling print leaves javascript blocked in parent window Print a div using javascript in angularJS single page application

Examples related to dataframe

Trying to merge 2 dataframes but get ValueError How to show all of columns name on pandas dataframe? Python Pandas - Find difference between two data frames Pandas get the most frequent values of a column Display all dataframe columns in a Jupyter Python Notebook How to convert column with string type to int form in pyspark data frame? Display/Print one column from a DataFrame of Series in Pandas Binning column with python pandas Selection with .loc in python Set value to an entire column of a pandas dataframe

Examples related to output-formatting

How to show full column content in a Spark Dataframe? Removing display of row names from data frame TypeError: not all arguments converted during string formatting python Alternate output format for psql Controlling number of decimal digits in print output in R How to print the full NumPy array, without truncation?

Examples related to rowname

Convert row names into first column Removing display of row names from data frame How to select some rows with specific rownames from a dataframe? Specifying row names when reading in a file Row names & column names in R