[R] question about lists

Duncan Murdoch murdoch.duncan at gmail.com
Fri Apr 20 00:46:19 CEST 2012


On 12-04-19 3:11 PM, harry mamaysky wrote:
> I am new to R, and I have been running into the following situation
> when I mistype a variable name in some code:
>
>> list1<- list( a=1, b=2 )
>> list2<- list( a=1 )
>> list2$b<- list1$c
>> list2
> $a
> [1] 1
>
> I would think at the point where I am trying to reference a field
> called "c" -- that does not exist -- in list1, there would be an error
> flagged.

Not intending to be snarky, but you should read the documentation, which 
does describe this.

> Instead, list1$c returns value NULL, and the assignment "list2$b<-
> NULL" does not return any error, but also does not create a field
> called "b" in list2.
>
> Is there a way to have R flag the reference to field  "c" in list1 as an error?

No there isn't.  You get a bit more support if you index by number: 
list1[[3]] will give an error (though assigning into list2[[2]] will 
make list2 bigger).

Duncan Murdoch



More information about the R-help mailing list