Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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

Now, let's check whether x is recursive

> is.recursive(x)
[1] FALSE

A recursive object has a list-like structure. A vector is not recursive, it is an atomic object instead, let's check

> is.atomic(x)
[1] TRUE

Therefore you get an error when applying $ to a vector (non-recursive object), use [ instead:

> x["ed"]

You can also use getElement

> getElement(x, "ed")
[1] 2
                I have come across the same error of  Error in object$y : $ operator is invalid for atomic vectors I have tried working thru your answer/suggestion and have not solved my problem. Maybe I don't understand your initial response. Could you please try to rephrase your answer using different a more complete example? Appreciate it
– mccurcio
                Jul 31, 2019 at 19:46
                @oaxacamatt my answer is related to data.frames or lists which are recursive objects, you are using vector and that's why my answer doesn't help you. You are wrong and my answer is correct, then your downvote is invalid.
– Jilber Urbina
                Jul 31, 2019 at 20:07

The reason you are getting this error is that you have a vector.

If you want to use the $ operator, you simply need to convert it to a data.frame. But since you only have one row in this particular case, you would also need to transpose it; otherwise bob and ed will become your row names instead of your column names which is what I think you want.

x <- c(1, 2)
names(x) <- c("bob", "ed")
x <- as.data.frame(t(x))
[1] 2
                yes  for 1 item as.list would be better.  I never use lists so I always forget about them.
– Dalupus
                Mar 10, 2016 at 19:33

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.

And, will give you output of x$ed as:
If you want bob and ed as column names then you need to transpose the dataframe like x <- as.data.frame(t(x)) So your code becomes

x<-c(1,2)
names(x)<- c("bob","ed")
x <- as.data.frame(t(x))

Now the output of x$ed is:
[1] 2

You get this error, despite everything being in line, because of a conflict caused by one of the packages that are currently loaded in your R environment.

So, to solve this issue, detach all the packages that are not needed from the R environment. For example, when I had the same issue, I did the following:

detach(package:neuralnet)

bottom line: detach all the libraries no longer needed for execution... and the problem will be solved.

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
                I think you have this backwards.  LIsts are recursive objects and can be accessed via $, numeric vectors are atomic and can't.  I'm not sure what you're trying to show here.
– Ben Bolker
                Mar 24, 2015 at 3:54
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.