Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 383
Dom Abbott Avatar asked Jun 26 '14 10:06

Dom Abbott


People also ask

How do I remove row names from a Dataframe in R?

Method 2: Assigning row names to NULL In case, the row names are explicitly assigned to the rows, then using rownames(df) to NULL, deletes the row names and uses row numbers to access the rows.

How do I delete a row label in pandas?

Pandas provide data analysts a way to delete and filter data frame using . drop() method. Rows can be removed using index label or column name using this method.

How do I delete a row name?

To remove the row names or column names from a matrix, we just need to set them to NULL, in this way all the names will be nullified.

How do I remove row numbers from a data frame?

To drop a row or column in a dataframe, you need to use the drop() method available in the dataframe. You can read more about the drop() method in the docs here. Rows are labelled using the index number starting with 0, by default. Columns are labelled using names.


2 Answers

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> 
like image 122
Anders Ellern Bilgrau Avatar answered Oct 09 '22 16:10

Anders Ellern Bilgrau


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

like image 45
Nedinator Avatar answered Oct 09 '22 18:10

Nedinator