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

后端 未结 3 1578
悲哀的现实
悲哀的现实 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?

    0 讨论(0)
  • 2020-12-21 15:32

    The colon : separates the starting point from the end point of a sequence. It is treated with higher priority than the + or - operator. Therefore,

    nrow(planets_df)-3:nrow(planets_df)
    

    is equal to

    nrow(planets_df) - (3:nrow(planets_df))
    

    If you want to have the last three entries using this syntax, you need to put the entire expression that defines the start of the sequence into brackets:

    planets_df[(nrow(planets_df)-3):nrow(planets_df),]
    
    0 讨论(0)
  • 2020-12-21 15:46

    nrow(planets_df)-3:nrow(planets_df) is being evaluated as 8 - (3:8) or

    (8-3) (8-4) (8-5) (8-6) (8-7) (8-8) = 5 4 3 2 1 0

    For future reference if you want the last few rows, use tail(planets_df, 3)

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