从 R 中的数据框中删除行名称的显示
使用元素的唯一行和列名称来引用数据框的行和列。数据框方法有一个属性 row.names ,它不会对数据框的现有结构进行任何修改,它只是忽略分配给行的名称。因此,不显示由行名称组成的第一列。默认情况下,分配给该属性的逻辑值为 TRUE。在本文中,我们将了解如何在 R 编程语言中不显示 DataFrame 行名称。
方法 1:使用 row.names = FALSE。
如果没有明确分配行名,则以 1 开头的行号被分配为数据帧的行名。以下 R 程序说明了在显示 row.names 属性设置为 FALSE 时的方法,因此,行名称在以下输出中不可见。
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)
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)
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)
输出:
[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
使用 row.names(df) 我们可以分配一个字符串变量对象作为数据帧的每一行名称。字符向量的长度应等于数据帧的长度。在这种情况下,默认行号被分配的行名覆盖。
电阻
# 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)
输出:
[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
方法 2:将行名称分配给 NULL
如果我们希望删除数据帧的行名称,那么我们可以在数据帧上使用 rownames() 方法将它们分配给 NULL。但是,这将导致整个数据帧的修改。如果行名被显式分配给行,然后使用 rownames(df) 为 NULL,删除行名并使用行号访问行。在这种情况下,最初使用行名称来引用行,但是一旦将 rownames(df) 分配为 null,就会删除对行名称的任何引用。
电阻
# 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)
输出:
[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