gpt4 book ai didi

R x$ed : $ operator is invalid for atomic vectors 中的错误

转载 作者:行者123 更新时间:2023-12-03 05:33:37 26 4
gpt4 key购买 nike

这是我的代码:

x<-c(1,2)
x
names(x)<- c("bob","ed")
x$ed

为什么我会收到以下错误?

Error in x$ed : $ operator is invalid for atomic vectors

最佳答案

从有关 $ 的帮助文件中(参见 ?"$"),您可以阅读:

$ is only valid for recursive objects, and is only discussed in the section below on recursive objects.

现在,让我们检查一下 x 是否是递归的

> is.recursive(x)
[1] FALSE

递归对象具有类似列表的结构。向量不是递归的,它是一个原子对象,让我们检查一下

> is.atomic(x)
[1] TRUE

因此,将 $ 应用于向量(非递归对象)时会出现错误,请改用 [:

> x["ed"]
ed
2

您还可以使用getElement

> getElement(x, "ed")
[1] 2

关于R x$ed : $ operator is invalid for atomic vectors 中的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23299684/

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