[R] Accessing named members of a list in an array

Peter Ehlers ehlers at ucalgary.ca
Sat Jun 30 19:40:11 CEST 2012


On 2012-06-30 09:04, David Winsemius wrote:
>
> On Jun 30, 2012, at 9:35 AM, mlell08 wrote:
>
>> Dear List,
>>
>> I've created a two-dimensional array which shall contain a value and
>> its
>> error, respectively.
>> These two values are concatenated in al list and bear the names "sl"
>> and
>> "sl_err"
>>
>> But I can't adress them using the $-notation.
>>
>> a<- array(list(NULL),dim=c(2,2))
>> a[[1,1]]<- c(a=2,b=3)
>> a[[1,1]]$a
>> ## Fehler in a[[1, 1]]$a : $ operator is invalid for atomic vectors
>> a[[1,1]]["a"]       # This works however.
>> ## a
>> ## 2
>>
>> I always thought these two methods of indexing are equal?
>
> You thought wrong (on two accounts as it happens). The "$" methods
> translate to "[[" with a quoted argument and there is no matrix/array
> equivalent since vectors loose their names (if they had any to begin
> with) when they are put into a matrix or array. The equivalent method
> to x$a is x[["a"]], not x["a"].

Actually, to be picky (and David knows this), quoting the
help page (and the OP should have read this):
" x$name is equivalent to x[["name", exact = FALSE]] ".

In my view, there is far too much eagerness to use "$" rather
than getting accustomed to the other, more powerful, extraction
methods.

Peter Ehlers



More information about the R-help mailing list