access an element with empty name

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

access an element with empty name

Serguei Sokol
Hi,

I came across a case where I cannot access a list element by its empty name.
Minimal example can be constructed as

     x=list("A", 1)
     names(x)=c("a", "")
     x[["a"]]
     #[1]  "A"
     x[[""]]
     #NULL
     x$`a`
     #[1]  "A"
     x$``
     # Error: attempt to use zero-length variable name
     # but we can still access the second element by its index
     x[[2]]
     #[1] 1

To my mind, it should be perfectly legal to access an element by an
empty name as we can have for example
     match("", names(x))
     #[1] 2
Hence a traditional question: is it a bug or feature?

Best,
Serguei.


 > sessionInfo()
R version 3.5.0 (2018-04-23)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Mageia 6

Matrix products: default
BLAS/LAPACK: /home/opt/OpenBLAS/lib/libopenblas_sandybridge-r0.3.0.dev.so

locale:
[1] C

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods base

loaded via a namespace (and not attached):
[1] compiler_3.5.0

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: access an element with empty name

Kurt Hornik-5
>>>>> Serguei Sokol writes:

> Hi,
> I came across a case where I cannot access a list element by its empty name.
> Minimal example can be constructed as

>      x=list("A", 1)
>      names(x)=c("a", "")
>      x[["a"]]
>      #[1]  "A"
>      x[[""]]
>      #NULL
>      x$`a`
>      #[1]  "A"
>      x$``
>      # Error: attempt to use zero-length variable name
>      # but we can still access the second element by its index
>      x[[2]]
>      #[1] 1

> To my mind, it should be perfectly legal to access an element by an
> empty name as we can have for example
>      match("", names(x))
>      #[1] 2
> Hence a traditional question: is it a bug or feature?

A feature according to the docs: ? Extract says

    Neither empty (‘""’) nor ‘NA’ indices match any names, not even
    empty nor missing names.

-k



> Best,
> Serguei.


>> sessionInfo()
> R version 3.5.0 (2018-04-23)
> Platform: x86_64-pc-linux-gnu (64-bit)
> Running under: Mageia 6

> Matrix products: default
> BLAS/LAPACK: /home/opt/OpenBLAS/lib/libopenblas_sandybridge-r0.3.0.dev.so

> locale:
> [1] C

> attached base packages:
> [1] stats     graphics  grDevices utils     datasets  methods base

> loaded via a namespace (and not attached):
> [1] compiler_3.5.0

> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: access an element with empty name

Serguei Sokol
Le 14/05/2018 à 15:55, Kurt Hornik a écrit :

>>>>>> Serguei Sokol writes:
>> Hi,
>> I came across a case where I cannot access a list element by its empty name.
>> Minimal example can be constructed as
>>       x=list("A", 1)
>>       names(x)=c("a", "")
>>       x[["a"]]
>>       #[1]  "A"
>>       x[[""]]
>>       #NULL
>>       x$`a`
>>       #[1]  "A"
>>       x$``
>>       # Error: attempt to use zero-length variable name
>>       # but we can still access the second element by its index
>>       x[[2]]
>>       #[1] 1
>> To my mind, it should be perfectly legal to access an element by an
>> empty name as we can have for example
>>       match("", names(x))
>>       #[1] 2
>> Hence a traditional question: is it a bug or feature?
> A feature according to the docs: ? Extract says
>
>      Neither empty (‘""’) nor ‘NA’ indices match any names, not even
>      empty nor missing names.
>

Thanks Kurt, I missed that one.

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel