删除 R 中的对象名称和暗名

Jesse John 2023年1月30日
  1. 使用 unname() 函数删除 R 中的对象名称和暗名
  2. 在可用时使用函数参数来删除 R 中的对象名称和暗名
  3. 使用 as.vector() 函数删除 R 中的对象名称和暗名
  4. 将 Names 和 Dimnames 设置为 NULL 以删除 R 中的对象名称和 Dimnames
  5. R 中数据框中的行名
  6. 结论
删除 R 中的对象名称和暗名

当我们只需要值时,R 为我们提供了删除名称和暗名的选项。我们将在本文中演示其中一些选项。

使用 unname() 函数删除 R 中的对象名称和暗名

unname() 函数从对象中删除名称和暗名。

示例代码:

# Calculate the 25th percentile.
a = quantile(c(2, 4, 6, 8), probs=0.25)
a # Displays the name.

names(a) # 25% is the name.

# We can remove the name by using the unname() function.
unname(a)

输出:

> # Calculate the 25th percentile.
> a = quantile(c(2, 4, 6, 8), probs=0.25)
> a # Displays the name.
25%
3.5
>
> names(a) # 25% is the name.
[1] "25%"
>
> # We can remove the name by using the unname() function.
> unname(a)
[1] 3.5

在可用时使用函数参数来删除 R 中的对象名称和暗名

一些函数允许我们创建没有名字的对象。函数签名会告诉我们是否可以跳过它的创建。

使用这样的函数,我们可以直接创建没有名字的对象。同样,我们将采用 quantile(),因为它具有 names 参数。

示例代码:

# Calculate the 60th percentile. Use names = FALSE.
b = quantile(c(2, 4, 6, 8), probs=0.6, names = FALSE)
b # There is no name
names(b)

输出:

> # Calculate the 60th percentile. Use names = FALSE.
> b = quantile(c(2, 4, 6, 8), probs=0.6, names = FALSE)
> b # There is no name
[1] 5.6
> names(b)
NULL

使用 as.vector() 函数删除 R 中的对象名称和暗名

手册 R 简介 的第 5.9 节指出,可以使用 as.vector() 将数组强制转换为简单的向量对象。此操作会删除名称。

示例代码:

# The summary has meaningful names.
c = summary(c(2, 4, 6, 8))
c
# This is just for illustration of the technique.
# We can remove the names using the as.vector() function.
as.vector(c)

输出:

> # The summary has meaningful names.
> c = summary(c(2, 4, 6, 8))
> c
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
    2.0     3.5     5.0     5.0     6.5     8.0
> # This is just for illustration of the technique.
> # We can remove the names using the as.vector() function.
> as.vector(c)
[1] 2.0 3.5 5.0 5.0 6.5 8.0

将 Names 和 Dimnames 设置为 NULL 以删除 R 中的对象名称和 Dimnames

由于可以分配名称和暗名,我们可以将它们设置为 NULL。

示例代码:

# The summary has names.
d = summary(c(2, 4, 6, 8))
d
names(d)

# Set the names to NULL.
names(d) = NULL

# Now, there are no names.
d
names(d)

输出:

> # The summary has names.
> d = summary(c(2, 4, 6, 8))
> d
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
    2.0     3.5     5.0     5.0     6.5     8.0
> names(d)
[1] "Min."    "1st Qu." "Median"  "Mean"    "3rd Qu." "Max."
>
> # Set the names to NULL.
> names(d) = NULL
>
> # Now there are no names.
> d
[1] 2.0 3.5 5.0 5.0 6.5 8.0
> names(d)
NULL

R 中数据框中的行名

如果我们在数据框上使用 dimnames(),我们会得到两个列表。一个是 names,它给出了列名;另一个是 row.names

在 R 中,数据框具有行名。如果我们不指定行名,R 会自动给出行名。

行名不能设置为 NULL。这样做会使 R 自动给出行名。

文档指出,当我们使用 force = TRUE 时,unname() 函数无法按预期工作。它会引发错误,至少在 R 版本 4.1.2 之前。

原因是无法通过将行名设置为 NULL 来删除它们。

它也不适用于 dplyr 的 tibbles,因为它们是数据帧。Tibbles 不能手动分配行名,但它们有。

示例代码:

library(dplyr)
X = as_tibble(data.frame(x=c(1:5),y=c(11:15)))
X

# column names.
names(X)

# A tibble has automatic row names.
row.names(X)

输出:

> library(dplyr)
> X = as_tibble(data.frame(x=c(1:5),y=c(11:15)))
> X
# A tibble: 5 x 2
      x     y
  <int> <int>
1     1    11
2     2    12
3     3    13
4     4    14
5     5    15
>
> # column names.
> names(X)
[1] "x" "y"
>
> # A tibble has automatic row names.
> row.names(X)
[1] "1" "2" "3" "4" "5"

可以使用 unname() 函数从数据框和小标题中删除列名。但是,我们必须记住,列名在许多数据分析中很有帮助。

结论

我们看到了一些删除对象名称和暗名的不同方法。

根据我们正在使用的对象,我们可以选择任何方便的选项来删除这些属性。

作者: Jesse John
Jesse John avatar Jesse John avatar

Jesse is passionate about data analysis and visualization. He uses the R statistical programming language for all aspects of his work.

相关文章 - R Object