Jonathan
Jonathan

Reputation: 5288

Why doesn't R allow $ operator on atomic vectors?

The following will throw the error "Error in v$a : $ operator is invalid for atomic vectors" (at least in R version 2.14.1):

v <- c(a='a',b='b')
v$a

Apparently, R previously allowed this, which makes me curious as to why.

EDIT: As pointed out below, v$a would have returned NULL in the earlier versions. Changed "fairly recently" to "previously", since I based this on old Internet forums and have been corrected below.

Upvotes: 6

Views: 27176

Answers (2)

Tyler Rinker
Tyler Rinker

Reputation: 110054

I believe the reason is that the use of v$a vs. v[['a']] is considered less safe.

EDIT: Check out this LINK for more details.

Upvotes: 8

Joshua Ulrich
Joshua Ulrich

Reputation: 176718

Third paragraph of the Details section of ?"$":

‘$’ is only valid for recursive objects, and is only discussed in the section below on recursive objects. Its use on non-recursive objects was deprecated in R 2.5.0 and removed in R 2.7.0.

R-2.7.0 was released in April, 2008. Four years is far from "recent", but maybe you have been a few versions behind for awhile?

Upvotes: 6

Related Questions