Removing display of row names from dataframe in R
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.
 "Original dataframe" Col_1 Col_2 Col_3 1 1 NA A 2 2 NA V 3 NA 3 j 4 0 8 y  "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.
 "Original dataframe" Col_1 Col_2 Col_3 ROW1 1 NA A ROW2 2 NA V ROW3 NA 3 j ROW4 0 8 y  "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.
 "Original dataframe" Col_1 Col_2 Col_3 ROW1 1 NA A ROW2 2 NA V ROW3 NA 3 j ROW4 0 8 y  "Modified dataframe" Col_1 Col_2 Col_3 1 1 NA A 2 2 NA V 3 NA 3 j 4 0 8 y
Please Login to comment...