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

前端 未结 6 1583
野的像风
野的像风 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 18:01

    Atomic collections are accessible by $

    Recursive collections are not. Rather the [[ ]] is used

     Browse[1]> is.atomic(list())
     [1] FALSE
    
     Browse[1]> is.atomic(data.frame())
     [1] FALSE
    
     Browse[1]> is.atomic(class(list(foo="bar")))
     [1] TRUE
    
     Browse[1]> is.atomic(c(" lang "))
     [1] TRUE
    

    R can be funny sometimes

     a = list(1,2,3)
     b = data.frame(a)
     d = rbind("?",c(b))
     e = exp(1)
     f = list(d)
     print(data.frame(c(list(f,e))))
    
       X1 X2 X3 X2.71828182845905
     1  ?  ?  ?          2.718282
     2  1  2  3          2.718282
    

提交回复
热议问题