Search code examples
rarraysmultiple-columns

generating a table in R, with defined parameters


I would like to generate a table with 2 columns, with pre-defined parameters for each column, like the picture below:

enter image description here

What I have in R code so far for the first column is:

current_point<-50
lower_bound<-40
upper_bound<-60
size<-2
seq(start,end,by=size)

I am able to generate an array from the lines, however, I am stuck when it comes to the second column after I define the needed parameters:

value_A<-3
fixed_point<-54

Can you please help? Million thanks.


Solution

  • This is a good fit for ifelse.

    col1 = seq(lower_bound,upper_bound,by=size)
    ifelse(col1 < fixed_point, col1 - fixed_point + value_A, value_A)
    #  [1] -11  -9  -7  -5  -3  -1   1   3   3   3   3
    

    Incidentally, I think you can reduce this with pmin:

    pmin(col1 - fixed_point + value_A, value_A)
    #  [1] -11  -9  -7  -5  -3  -1   1   3   3   3   3
    

    This is doing a vectorized (element-by-element) minimum.

    Even more code reduction, in the form of algebraic factoring:

    pmin(col1 - fixed_point, 0) + value_A
    #  [1] -11  -9  -7  -5  -3  -1   1   3   3   3   3