gpt4 book ai didi

r - 没有列名的 data.frames 的奇怪行为

转载 作者:行者123 更新时间:2023-12-04 02:14:17 27 4
gpt4 key购买 nike

没有列名的 data.frames 存在意外行为。以下按预期工作:

df <- data.frame(a = 1:5, b = 5:9)
df + 1
## a b
## 1 2 6
## 2 3 7
## 3 4 8

但如果我们删除列名,则行为很奇怪:
names(df) <- NULL
df + 1
## data frame with 0 columns and 0 rows

如果使用 unname 删除名称,也会发生同样的情况。 , setNames .关于为什么会发生这种情况以及(出于某种原因)预期行为的任何想法?

编辑 :
所以有记载,无名 data.frame s 有不受支持的结果(感谢@neilfws、@Suren)但我也对为什么会发生这种情况感兴趣。我试图找到使这个简单示例停止的实际 c (?) 代码。

最佳答案

data.frame 的文档中, 它说:

The column names should be non-empty, and attempts to use empty names will have unsupported results.



因此,如果列名称为空,则预期结果可能不是所需的。

关于r - 没有列名的 data.frames 的奇怪行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49874365/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com