14404

Subsetting R with dynamic variables [duplicate]

<div class="question-status question-originals-of-duplicate">

This question already has an answer here:

    <li> Brackets make a vector different. How exactly is vector expression evaluated? <span class="question-originals-answer-count"> 3 answers </span> </li> </ul>

    I have the below example code. I have a dataframe ts which has 16 rows. when I subset with actual numbers it works fine but when I subset with calculated numbers why is my code behaving weirdly ?

    Can anyone please explain me what's wrong in this?

    <strong>Case1:</strong>

    > a [1] 12 > c [1] 16 > ts$trend[13:16] [1] 21.36926 21.48654 21.60383 21.72111 > ts$trend[a+1:c] [1] 21.36926 21.48654 21.60383 21.72111 NA NA NA NA NA NA NA NA [13] NA NA NA NA

    <strong>Case 2:</strong>

    > b [1] 4 > temp[1: 8] [1] 1 2 3 4 5 6 7 8 > temp[1: b+b] [1] 5 6 7 8

    Answer1:

    This is a case of operator precedence. It can be avoided by using brackets

    temp[1:(b+b)] #[1] 1 2 3 4 5 6 7 8 <hr>

    If we check the problem in OP's code

    1:b #[1] 1 2 3 4 (1:b) + b #[1] 5 6 7 8

    So, the operator precedence happens here by evaluating 1:b followed by adding the b.

    This is well described in ?Syntax

    :: ::: access variables in a namespace $ @ component / slot extraction [ [[ indexing ^ exponentiation (right to left) - + unary minus and plus : sequence operator %any% special operators (including %% and %/%) * / multiply, divide + - (binary) add, subtract < > <= >= == != ordering and comparison ! negation & && and | || or ~ as in formulae -> ->> rightwards assignment <- <<- assignment (right to left) = assignment (right to left) ? help (unary and binary)

    data

    temp <- 1:10 b <- 4

    Answer2:

    R doesn't care about they way you space expressions. Things are evaluated according to a strict precedence scheme. Things in parentheses are done first. So:

    > 1: b+b [1] 5 6 7 8

    because addition has lower precedence than ":". The 1:b is evaluated first, and then b is added. So you get:

    > (1:b)+b [1] 5 6 7 8

    If you want the alternative, parenthesise things:

    > 1:(b+b) [1] 1 2 3 4 5 6 7 8

    I'd suggest you also parenthesise (1+b):b if that is ever what you want - the brackets make no difference but they aid readability for anyone who forgets the precedence rules.

Recommend

  • R function to print table with stationarity test results
  • Which's better: MDI children, or modeless dialogs?
  • UIButton Tag not being assigned indexPath.row after 6 cells
  • pandas computation in each group
  • NSIS decompiler
  • How to estimate the Kalman Filter with 'KFAS' R package, with an AR(1) transition equation
  • How to 'create temp table as select' in Slick?
  • How load cv2.KeyPoint and Descriptors correctly on OpenCV 3 with Python 3?
  • Plotting line graph with factors in R
  • RxJava debounce by arbitrary value
  • Paperclip, set path outside of rails root folder
  • How to model a transition system with SPIN
  • Importing jscolor library in angular 2
  • How to make Safari send if-modified-since header?
  • Release, debug version and Authorization Google?
  • jquery mobile loadPage not working
  • Apache 2.4 - remove | delete | uninstall
  • How to pass list parameters for each object using Spring MVC?
  • How to delete a row from a dynamic generate table using jquery?
  • Python: how to group similar lists together in a list of lists?
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • Codeigniter doesn't let me update entry, because some fields must be unique
  • Free memory of cv::Mat loaded using FileStorage API
  • Trying to get generic when generic is not available
  • Getting Messege Twice Using IMvxMessenger
  • Setting background image for body element in xhtml (for different monitors and resolutions)
  • Change div Background jquery
  • How to get Windows thread pool to call class member function?
  • Bitwise OR returns boolean when one of operands is nil
  • XCode 8, some methods disappeared ? ex: layoutAttributesClass() -> AnyClass
  • JaxB to read class hierarchy
  • Easiest way to encapsulate a HTML5 webpage into an android app?
  • Django query for large number of relationships
  • Busy indicator not showing up in wpf window [duplicate]
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Unable to use reactive element in my shiny app
  • Conditional In-Line CSS for IE and Others?