[datatable-help] Vectors & Ifelse Logic - Won't Populate Vector

bigappleanalyst aroos at terpmail.umd.edu
Sat Mar 3 05:19:36 CET 2018


I'm creating a function that takes two inputs: delta, and length. The
function modifies an existing vector - let's call this vector base - by
delta for 'length' specified elements.

I need to create a new vector, let's call it 'adjusted'. This vector,
adjusted, needs to populate based on one condition: If the index value for
the base vector is less than the length parameter specified in the function
input, then I want to increase the base vector by delta for all elements
less than length. All elements in the base vector greater than the specified
length parameter are to remain the same as in base vector.

Here is an example of my code: I have tried to implement the logic in two
ways. 1. Using an ifelse because it allows for vectorized output. 2. Using
an if else logic.

    adjusted <- vector() #initialize the vector.

#Create a function. 
Adjustment <- function(delta, length) {

  ifelse((index < length), (adjusted <- base + delta), (adjusted <- base)) 

  head(adjusted)
}

    #Method 2. 
   Adjustment <- function(delta, length) {

      if (index < length) {
            adjusted <- base + delta
        }
      else {adjusted <- base}

      head(adjusted)
    }
Where I am experiencing trouble is with my newly created vector, adjusted,
will not populate. I'm not quite sure what is happening - I am very new to R
and any insight would be much appreciated.



--
Sent from: http://r.789695.n4.nabble.com/datatable-help-f2315188.html


More information about the datatable-help mailing list