list members can be accessed with partial name? Is this a feature?

前端 未结 2 466
轻奢々
轻奢々 2021-01-04 23:53

Consider this R code

> l = list(key = 1)
> l$k
[1] 1
> l$ke
[1] 1
> l[[\'k\']]
NULL
> names(l)
[1] \"key\"

Does this mean th

2条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-05 00:46

    Yes, $ will do partial matching. Check the R document of the $ function by typing in the console

    ?`$`
    

    In the help document it says:

    Both [[ and $ select a single element of the list. The main difference is that $ does not allow computed indices, whereas [[ does. x$name is equivalent to x[["name", exact = FALSE]]. Also, the partial matching behavior of [[ can be controlled using the exact argument.

    According to Hadley Wickham's book "Advanced R", you can turn off the partial matching of $ by setting the global option warnPartialMatchDollar to TRUE, but it may affect behavior in other code you have loaded, e.g. from a package.

提交回复
热议问题