Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading