Brackets make a vector different. How exactly is vector expression evaluated?

后端 未结 3 1628
悲哀的现实
悲哀的现实 2020-12-21 15:14

I have a data frame as follows:

planets               type diameter rotation rings
Mercury Terrestrial planet    0.382    58.64 FALSE 
Venus   Terrestrial pl         


        
3条回答
  •  别那么骄傲
    2020-12-21 15:30

    The colon operator will take precedence over the arithmetic operations. It is always best to experiment with examples to internalize the logic:

    2*2:6-1
    

    What answer should we expect? Some would say 4 5. The thinking is that it will simplify to 2*2=4 and 6-1=5, therefore 4:5.

    2*2:6-1
    [1]  3  5  7  9 11
    

    This answer will surprise anyone who hasn't considered the order of operations in play. The expression 2*2:6-1 is simplified differently. The sequence 2:6 is carried out first, then the multiplication, and finally the addition. We could write it out as 2 * (2 3 4 5 6), which is 4 6 8 10 12 and subtract 1 from that to get 3 5 7 9 11.

    By grouping with parantheses we can control the order of operations as we would do similarly in basic arithmetic to get the answer that we first expected.

    (2*2):(6-1)
    [1] 4 5
    

    You can apply this reasoning to your example to investigate the seemingly odd behavior of the : operator.

    Now that you know the secret codes, what should we expect from (2*2):6-1?

提交回复
热议问题