1

Can someone explain why I get NULL in the following ?

> a =  list( 'key'='val')
> a$'key'
[1] "val"
> a$key
[1] "val"
> key_var = 'key'
> a$key_var
NULL

I know that I can do

> a[[key_val]]
[1] "val"

but I think double brackets can get messy (especially when working with nested lists, etc).

dermen
  • 5,252
  • 4
  • 23
  • 34
  • 3
    For passing object names to subset you should use `[` and not `$`. Please check `?'$'` – akrun Jun 19 '15 at 05:10
  • 2
    Look at this case: `key_var = 'key'; a = list( key='val', key_var="hello"); a$key_var`. What should be returned? That's just how the `$` operator works. You can't use it with variables. The `[[` syntax is perfect for this operation. I'm sorry you think they are messy. – MrFlick Jun 19 '15 at 05:11

0 Answers0