Hi

ifelse shall work just fine.

x<-1:10
a<-rnorm(10)
b<-rnorm(10)+100
tinf<-5
ifelse(x<tinf, a,b)
[1]  -0.32157732  -0.11065857   0.07127818   0.35928359 100.06200507
[6]  98.77752570 100.92743376  99.73918386  99.79837539 101.32640626

It is only your value of Tinf which is 0.5 compared to TIME which is 0:10 and therefore your logical construction selects only values lower than Tinf

see
TIME6<=Tinf
result

If you encounter strange result of complicated code skin it to bones and separate to pieces line by line and even to line parts if possible. This will give you insight how your code really works.

Just like disassembling old clock (those which have cogwheels) to see and understand how it works.

Regards
Petr

From: Andras Farkas [mailto:motyocska@yahoo.com]
Sent: Friday, November 30, 2012 5:37 PM
To: PIKAL Petr; r-help@r-project.org
Subject: Re: [R] help on "stacking" matrices up

Petr,

thanks for the solution, putting the zeros in there makes it work just how I want it. Also, thanks for picking up on the error in my code below... I did not notice that it only changes the 1st value, and yes, your assumption is right: there is a chance where more values should be changed based on Tinf... I have tryed to work with if ... else..., but that does not give me correct answer either, to my suprise... Would you be willing to provide some insigths on how I could go about solving that?

thanks,

Andras

From: PIKAL Petr <petr.pikal@precheza.cz<mailto:petr.pikal@precheza.cz>>
To: Andras Farkas <motyocska@yahoo.com<mailto:motyocska@yahoo.com>>; "r-help@r-project.org<mailto:r-help@r-project.org>" <r-help@r-project.org<mailto:r-help@r-project.org>>
Sent: Friday, November 30, 2012 10:58 AM
Subject: RE: [R] help on "stacking" matrices up

Hi Andras

Now it works but I suppose you shall simplify or explain it a bit more.

CONC7<-c(rep(0,3),CONC7)
CONC8<-c(rep(0,6),CONC8)
CONC6+CONC7+CONC8
          [,1]
[1,] 0.000000
[2,] 2.330056
[3,] 2.133774
[4,] 1.954027
[5,] 4.119477
[6,] 3.772456
[7,] 3.454667
[8,] 5.493705
[9,] 5.030920
[10,] 4.607120

gives you desired result easier. So for summing CONC objects just add to beginning of each object so many zeroes as there are missing values. You can check dimension by

dim or nrow and use it to estimate how many zeroes you need to add.

Besides, you probably do not need matrices, vectors can do it too.

This
CONC6 <-ifelse(TIME6<=Tinf,CDURINF6,CAFTINF6)
in your case just change only the first value, but I presume in reality you can have Tinf such that it results in changing more values.

If this is not you are after, please be more specific.

Regards
Petr

From: Andras Farkas [mailto:motyocska@yahoo.com]
Sent: Friday, November 30, 2012 3:52 PM
To: PIKAL Petr; r-help@r-project.org<mailto:r-help@r-project.org>
Subject: Re: [R] help on "stacking" matrices up

Petr,

sorry, here is a better example that should work:

Dose<-200
Tinf <-0.5
INTERVAL <-3
TIME8 <-matrix(c((0*INTERVAL):(1*INTERVAL)))
TIME7 <-matrix(c((0*INTERVAL):(2*INTERVAL)))
TIME6 <-matrix(c((0*INTERVAL):(3*INTERVAL)))
CDURINF6 <-((Dose/Tinf)*(1/(0.088*76.9)))*(1-exp(-0.088*TIME6))
CAFTINF6 <-((Dose/Tinf)*(1/(0.088*76.9)))*(1-exp(-0.088*Tinf))*(exp(-0.088*(TIME6)))
CONC6 <-ifelse(TIME6<=Tinf,CDURINF6,CAFTINF6)
CDURINF7 <-((Dose/Tinf)*(1/(0.088*76.9)))*(1-exp(-0.088*TIME7))
CAFTINF7 <-((Dose/Tinf)*(1/(0.088*76.9)))*(1-exp(-0.088*Tinf))*(exp(-0.088*(TIME7)))
CONC7 <-ifelse(TIME7<=Tinf,CDURINF7,CAFTINF7)
CDURINF8 <-((Dose/Tinf)*(1/(0.088*76.9)))*(1-exp(-0.088*TIME8))
CAFTINF8 <-((Dose/Tinf)*(1/(0.088*76.9)))*(1-exp(-0.088*Tinf))*(exp(-0.088*(TIME8)))
CONC8 <-ifelse(TIME8<=Tinf,CDURINF8,CAFTINF8)
CONCF <-matrix(c(CONC6[1,],CONC6[2,],CONC6[3,],CONC6[4,]+CONC7[1,],CONC6[5,]+CONC7[2,],CONC6[6,]+CONC7[3,],CONC6[7,]+CONC7[4,]+CONC8[1,],CONC6[8,]+CONC7[5,]+CONC8[2,],CONC6[9,]+CONC7[6,]+CONC8[3,],CONC6[10,]+CONC7[7,]+CONC8[4,]))
plot(TIME6,CONCF,type="b")

the plot is what it should look like, keeping in mind that the INTERVAL will change, and that is the part i amtrying to figure out, ie: how can i make that "dynamic". Is this better?

thanks,

Andras

From: PIKAL Petr <petr.pikal@precheza.cz<mailto:petr.pikal@precheza.cz>>
To: Andras Farkas <motyocska@yahoo.com<mailto:motyocska@yahoo.com>>; "r-help@r-project.org<mailto:r-help@r-project.org>" <r-help@r-project.org<mailto:r-help@r-project.org>>
Sent: Friday, November 30, 2012 8:51 AM
Subject: RE: [R] help on "stacking" matrices up

Hi

try to make your example easier and reproducible.

I get

> CONCF <-matrix(c(CONC1+CONC2+CONC3+CONC4+CONC5+CONC6+CONC7+CONC8))
Error in CONC1 + CONC2 : non-conformable arrays

so I cannot understand what shall the final plot look like.

Regards

Petr


> -----Original Message-----
> From: r-help-bounces@r-project.org<mailto:r-help-bounces@r-project.org> [mailto:r-help-bounces@r-
> project.org<mailto:r-help-bounces@r-%0b%3e%20project.org>] On Behalf Of Andras Farkas
> Sent: Friday, November 30, 2012 12:51 PM
> To: r-help@r-project.org<mailto:r-help@r-project.org>
> Subject: [R] help on "stacking" matrices up
>
> Dear All,
>
> #I have the following code
>
> Dose<-1000
> Tinf <-0.5
> INTERVAL <-8
> TIME8 <-matrix(c((0*INTERVAL):(1*INTERVAL)))
> TIME7 <-matrix(c((0*INTERVAL):(2*INTERVAL)))
> TIME6 <-matrix(c((0*INTERVAL):(3*INTERVAL)))
> TIME5 <-matrix(c((0*INTERVAL):(4*INTERVAL)))
> TIME4 <-matrix(c((0*INTERVAL):(5*INTERVAL)))
> TIME3 <-matrix(c((0*INTERVAL):(6*INTERVAL)))
> TIME2 <-matrix(c((0*INTERVAL):(7*INTERVAL)))
> TIME1 <-matrix(c((0*INTERVAL):(8*INTERVAL)))
> CDURINF1 <-((Dose/Tinf)*(1/(0.088*76.9)))*(1-exp(-0.088*TIME1))
> CAFTINF1 <-((Dose/Tinf)*(1/(0.088*76.9)))*(1-exp(-0.088*Tinf))*(exp(-
> 0.088*(TIME1)))
> CONC1 <-ifelse(TIME1<=Tinf,CDURINF1,CAFTINF1)
> CDURINF2 <-((Dose/Tinf)*(1/(0.088*76.9)))*(1-exp(-0.088*TIME2))
> CAFTINF2 <-((Dose/Tinf)*(1/(0.088*76.9)))*(1-exp(-0.088*Tinf))*(exp(-
> 0.088*(TIME2)))
> CONC2 <-ifelse(TIME2<=Tinf,CDURINF2,CAFTINF2)
> CDURINF3 <-((Dose/Tinf)*(1/(0.088*76.9)))*(1-exp(-0.088*TIME3))
> CAFTINF3 <-((Dose/Tinf)*(1/(0.088*76.9)))*(1-exp(-0.088*Tinf))*(exp(-
> 0.088*(TIME3)))
> CONC3 <-ifelse(TIME3<=Tinf,CDURINF3,CAFTINF3)
> CDURINF4 <-((Dose/Tinf)*(1/(0.088*76.9)))*(1-exp(-0.088*TIME4))
> CAFTINF4 <-((Dose/Tinf)*(1/(0.088*76.9)))*(1-exp(-0.088*Tinf))*(exp(-
> 0.088*(TIME4)))
> CONC4 <-ifelse(TIME4<=Tinf,CDURINF4,CAFTINF4)
> CDURINF5 <-((Dose/Tinf)*(1/(0.088*76.9)))*(1-exp(-0.088*TIME5))
> CAFTINF5 <-((Dose/Tinf)*(1/(0.088*76.9)))*(1-exp(-0.088*Tinf))*(exp(-
> 0.088*(TIME5)))
> CONC5 <-ifelse(TIME5<=Tinf,CDURINF5,CAFTINF5)
> CDURINF6 <-((Dose/Tinf)*(1/(0.088*76.9)))*(1-exp(-0.088*TIME6))
> CAFTINF6 <-((Dose/Tinf)*(1/(0.088*76.9)))*(1-exp(-0.088*Tinf))*(exp(-
> 0.088*(TIME6)))
> CONC6 <-ifelse(TIME6<=Tinf,CDURINF6,CAFTINF6)
> CDURINF7 <-((Dose/Tinf)*(1/(0.088*76.9)))*(1-exp(-0.088*TIME7))
> CAFTINF7 <-((Dose/Tinf)*(1/(0.088*76.9)))*(1-exp(-0.088*Tinf))*(exp(-
> 0.088*(TIME7)))
> CONC7 <-ifelse(TIME7<=Tinf,CDURINF7,CAFTINF7)
> CDURINF8 <-((Dose/Tinf)*(1/(0.088*76.9)))*(1-exp(-0.088*TIME7))
> CAFTINF8 <-((Dose/Tinf)*(1/(0.088*76.9)))*(1-exp(-0.088*Tinf))*(exp(-
> 0.088*(TIME8)))
> CONC8 <-ifelse(TIME8<=Tinf,CDURINF8,CAFTINF8)
> CONCF <-matrix(c(CONC1+CONC2+CONC3+CONC4+CONC5+CONC6+CONC7+CONC8))
> plot(CONCF,type="l")
>
> I am trying to plot CONCF in a specific way. Let me try to explain: I
> would like to plot it so that the x axis goes from 0 to 8*INTERVAL
> (here the INTERVAL represents time in hours), and then each "segments"
> of the matrix  would  be added to the sum of the previous ones at a
> specific moment in time. Assuming a much shorter example this is what
> it should be like where according to the same equations above each
> value of CONCi was calculated at 1 h intervals:
> INTERVAL <=3
> time points here where the values are calculated is 0:(3*INTERVAL) for
> CONC1, 0:(2*INTERVAL) for CONC2, and 0:(1*INTERVAL) for CONC3, all
> calculated at 1 hour intervals
>
> and lets assume calculated values are:
>
> CONC1 <-c(0,1,2,3,4,5,6,7,8,9)
> CONC2 <-c( 11,12,13,14,15,16,17)
> CONC3 <-(21,22,23,24)
> .
> .
> .
>
> so I would like to get CONCF that would have the following values:
> CONCF <-c(0,1,2,3+11,4+12,5+13,6+14,7+15,8+16,9+17,
> 6+14+21,7+15+22,8+16+23,9+17+23) please note that CONC2 was started to
> be added to CONC1 at 1*INTERVAL value estimated for CONC1, ie CONC1 has
> the value of 3 at hour 3, and I need to add the value of CONC2 at hour
> zero to it, and so on...
> hope I made it clear enough and thanks for the help,
>
> Andras
>     [[alternative HTML version deleted]]



	[[alternative HTML version deleted]]


