Removing display of row names from dataframe in R
Last Updated :
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 R
data_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 dataframe
print("Modified dataframe")
# without displaying rownames
print(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 R
data_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 dataframe
row.names(data_frame) <- c("ROW1", "ROW2", "ROW3", "ROW4")
print("Original dataframe")
print(data_frame)
# printing modified dataframe
print("Modified dataframe")
# without displaying rownames
print(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 R
data_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 dataframe
row.names(data_frame) <- c("ROW1","ROW2","ROW3","ROW4")
print("Original dataframe")
print(data_frame)
# assigning the rownames to null
rownames(data_frame) <- NULL
# printing modified dataframe
print("Modified dataframe")
# without displaying rownames
print(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
Similar Reads
Remove First Row of DataFrame in R In this article, we are going to see how to remove the first row from the dataframe. We can remove first row by indexing the dataframe. Syntax: data[-1,] where -1 is used to remove the first row which is in row position Example 1: R program to create a dataframe with 2 columns and delete the first
1 min read
How to remove empty rows from R dataframe? A dataframe can contain empty rows and here with empty rows we don't mean NA, NaN or 0, it literally means empty with absolutely no data. Such rows are obviously wasting space and making data frame unnecessarily large. This article will discuss how can this be done. To remove rows with empty cells w
1 min read
How to change Row Names of DataFrame in R ? The rows are stacked together, each denoted by a unique name. By default, the integer identifiers beginning from 1 to the number of rows are assigned to the data frame by default. The task here is to change the Rows names in given dataframe using R programming. Dataset in use: First SecondThird1a72a
3 min read
How to Remove Duplicate Rows in R DataFrame? In this article, we will discuss how to remove duplicate rows in dataframe in R programming language. Dataset in use:Method 1: Using distinct() This method is available in dplyr package which is used to get the unique rows from the dataframe. We can remove rows from the entire which are duplicates a
2 min read
Drop row(s) by number from given DataFrame in R In this article, we will be discussing the approaches to drop rows by a number from a given Data Frame in R language. Dropping of rows from a data frame is simply used to remove the unwanted rows in the data frame. Method 1: Using minus(-) sign In this method, the user needs to provide the index of
1 min read
How to Retrieve Row Numbers in R DataFrame? In this article, we will discuss how to Retrieve Row Numbers in R Programming Language. The dataframe column can be referenced using the $ symbol, which finds its usage as data-frame$col-name. The which() method is then used to retrieve the row number corresponding to the true condition of the speci
2 min read