R: Why is the [[ ]] approach for subsetting a list faster than using $?

后端 未结 1 1087
不思量自难忘°
不思量自难忘° 2020-12-13 13:01

I\'ve been working on a few projects that have required me to do a lot of list subsetting and while profiling code I realised that the object[[\"nameHere\"]] approach to sub

相关标签:
1条回答
  • 2020-12-13 13:23

    Function [[ first goes through all elements trying for exact match, then tries to do partial match. The $ function tries both exact and partial match on each element in turn. If you execute:

    system.time (
        for (i in 1:10000) {
         a.long.list[["something9973", exact=FALSE]]
         }
    )
    

    i.e., you are running a partial match where there is no exact match, you will find that $ is in fact ever so slightly faster.

    0 讨论(0)
提交回复
热议问题