Removing display of row names from dataframe in R - GeeksforGeeks

16 May, 2021
The dataframe rows and columns are referenced using unique row and column names for the elements. The dataframe method has an attribute row.names that don’t make any modification to the existing structure of the dataframe, it just ignores the names assigned to rows. As a result, the first column consisting of row names is not displayed. By default, the logical value assigned to this attribute is TRUE. In this article, we are going to see how to not display DataFrame row names in R Programming Language.
Method 1: Using row.names = FALSE.
In case, the row names are not assigned explicitly, row numbers beginning with 1 are assigned as row names of the dataframe. The following R program illustrates the method where while displaying the row.names attribute is set to FALSE and hence, row names are not visible in the following output. 
R
# declaring a dataframe in Rdata_frame = data.frame("Col_1" = c(1, 2, NA, 0),                        "Col_2" = c( NA, NA, 3, 8),                        "Col_3" = c("A", "V", "j", "y"))  print("Original dataframe")print(data_frame)  # printing modified dataframeprint("Modified dataframe")  # without displaying rownamesprint(data_frame,row.names=FALSE)
Output:
[1] "Original dataframe"
  Col_1 Col_2 Col_3
1     1    NA     A
2     2    NA     V
3    NA     3     j
4     0     8     y
[1] "Modified dataframe"
Col_1 Col_2 Col_3
    1    NA     A
    2    NA     V
   NA     3     j
    0     8     y
Using row.names(df) we can assign a string variable object as each row name of the dataframe. The length of the character vector should be equivalent to the length of the dataframe. In this case, the default row numbers are overwritten by the row names assigned. 
R
# declaring a dataframe in Rdata_frame = data.frame("Col_1" = c(1, 2, NA, 0),                        "Col_2" = c( NA, NA, 3, 8),                         "Col_3" = c("A", "V", "j", "y"))  # assigning row names to dataframerow.names(data_frame) <- c("ROW1", "ROW2", "ROW3", "ROW4")print("Original dataframe")print(data_frame)  # printing modified dataframeprint("Modified dataframe")  # without displaying rownamesprint(data_frame, row.names = FALSE)
Output:
[1] "Original dataframe"
    Col_1 Col_2 Col_3
ROW1     1    NA     A
ROW2     2    NA     V
ROW3    NA     3     j
ROW4     0     8     y
[1] "Modified dataframe"
Col_1 Col_2 Col_3
    1    NA     A
    2    NA     V
   NA     3     j
    0     8     y
Method 2: Assigning row names to NULL
In case, we wish to delete the row names of the dataframe, then we can assign them to NULL using the rownames() method over the dataframe. However, this will lead to the modification in the entire dataframe. 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. In this case, initially the row names are used to reference to rows, but as soon as the rownames(df) is assigned to null, any references to the row names is removed. 
R
# declaring a dataframe in Rdata_frame = data.frame("Col_1" = c(1, 2, NA, 0),                         "Col_2" = c( NA, NA, 3, 8),                        "Col_3" = c("A", "V", "j", "y"))  # assigning row names to dataframerow.names(data_frame) <- c("ROW1","ROW2","ROW3","ROW4")print("Original dataframe")print(data_frame)  # assigning the rownames to nullrownames(data_frame) <- NULL  # printing modified dataframeprint("Modified dataframe")  # without displaying rownamesprint(data_frame)
Output:
[1] "Original dataframe"
      Col_1 Col_2 Col_3
ROW1     1    NA     A
ROW2     2    NA     V
ROW3    NA     3     j
ROW4     0     8     y
[1] "Modified dataframe"
   Col_1 Col_2 Col_3
1     1    NA     A
2     2    NA     V
3    NA     3     j
4     0     8     y
Picked
R DataFrame-Programs
R-DataFrame
R Language
R Programs
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Change Color of Bars in Barchart using ggplot2 in R
How to Change Axis Scales in R Plots?
Group by function in R using Dplyr
How to Split Column Into Multiple Columns in R DataFrame?
How to filter R DataFrame by values in a column?
How to Split Column Into Multiple Columns in R DataFrame?
How to filter R DataFrame by values in a column?
How to filter R dataframe by multiple conditions?
Replace Specific Characters in String in R
Convert Matrix to Dataframe in R