R Error in x$ed : $ operator is invalid for atomic vectors
R Error in x$ed : $ operator is invalid for atomic vectors
From the help file about $
(See ?$
) you can read:
$ is only valid for recursive objects, and is only discussed in the section below on recursive objects.
Now, lets 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, lets check
> is.atomic(x)
[1] TRUE
Therefore you get an error when applying $
to a vector (non-recursive object), use [
instead:
> x[ed]
ed
2
You can also use getElement
> getElement(x, ed)
[1] 2
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)
x
names(x) <- c(bob, ed)
x <- as.data.frame(t(x))
x$ed
[1] 2
R Error in x$ed : $ operator is invalid for atomic vectors
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.