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

前端 未结 6 1587
野的像风
野的像风 2020-11-29 17:45

Here is my code:

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

Why do I get the following error?

Error in x$ed

6条回答
  •  悲&欢浪女
    2020-11-29 17:57

    Because $ does not work on atomic vectors. Use [ or [[ instead. From the help file for $:

    The default methods work somewhat differently for atomic vectors, matrices/arrays and for recursive (list-like, see is.recursive) objects. $ is only valid for recursive objects, and is only discussed in the section below on recursive objects.

    x[["ed"]] will work.

提交回复
热议问题