[R] appending
Afshartous, David
afshart at exchange.sba.miami.edu
Thu Jun 15 17:44:34 CEST 2006
Hi Hank,
Thanks so much for looking at this.
Yes, it does what I want and is much more efficient than my code.
I have a couple questions, please see my inserted comments after your
tapply statement and within your for loop. Also, how do you accomplish
what you mentioned RE the concatation in the naming?
cheers,
Dave
Ps - just noticed that a quick fix to my code would be to use unlist
towards my original
result delta.patient, then the creative indexing I used would not have
been needed (but your code still more efficient).
comp.CAND.frm <- read.table("comp.CAND.frm.frm", header=TRUE)
names(comp.CAND.frm) <- make.names(gsub("_", ".", names
(comp.CAND.frm)), unique=TRUE)
#
creatine.function.new.R.help = function(delta.0.Y.0=50, gamma=40,
dat=comp.CAND.frm)
{
## function to calcuate the delta.i, i.e. the percent
## leftover ## gamma = rate of Cr going into bucket, e.g., mg/hr
## delta.0.Y.0 = product of delta.0 and Y.0 at baseline ##
unlist(
tapply(dat$Ucr, dat$Patient.no, function(Cs)
{ ## Cs is the argument to the function
defined
## for this tapply statement
## Cs will equal Ucr?
n.obs <- length(Cs)
Y=numeric(n.obs+1)
Y[1] = delta.0.Y.0 + gamma
delta = numeric(n.obs)
for (i in 1:n.obs) delta[i] <- ## why delta[i] outside
the {}?
{
d <- (Y[i] - Cs[i])/Y[i] ## why not just call
this d[i]?
Y[i+1] = delta[i] * Y[i] + gamma
d ## the output of each iteration; set equal to
d[i] each time?
}
delta # result of function(Cs) used in tapply
}
)
)
}
dim(comp.CAND.frm)
6*24
creatine.function.new()
### The names are the concatenation of the patient and the observation
## e.g. patient 24 observation 6 is labeled 246.
-----Original Message-----
From: Martin Henry H. Stevens [mailto:HStevens at muohio.edu]
Sent: Thursday, June 15, 2006 7:00 AM
To: Afshartous, David
Cc: r-help at stat.math.ethz.ch
Subject: Re: [R] appending
Hi Dave,
Does this do what you want?
Hank
comp.CAND.frm <- read.table("comp.CAND.frm.frm", header=TRUE)
names(comp.CAND.frm) <- make.names(gsub("_", ".", names
(comp.CAND.frm)), unique=TRUE)
creatine.function.new = function(delta.0.Y.0=50, gamma=40,
dat=comp.CAND.frm) {
## function to calcuate the delta.i, i.e. the percent
## leftover ## gamma = rate of Cr going into bucket, e.g., mg/hr
## delta.0.Y.0 = product of delta.0 and Y.0 at baseline ##
##
unlist(
tapply(dat$Ucr, dat$Patient.no, function(Cs) {
n.obs <- length(Cs)
Y=numeric(n.obs+1)
Y[1] = delta.0.Y.0 + gamma
delta = numeric(n.obs)
for (i in 1:n.obs) delta[i] <- {
d <- (Y[i] - Cs[i])/Y[i]
Y[i+1] = delta[i] * Y[i] + gamma
d
}
delta}
)
)
}
dim(comp.CAND.frm)
6*24
creatine.function.new()
### The names are the concatenation of the patient and the observation
## e.g. patient 24 observation 6 is labeled 246.
On Jun 14, 2006, at 2:35 PM, Afshartous, David wrote:
> Hank,
> Attached is the dataframe that can be supplied for the argument
> comp.CAND.frm. The argument for comp.LIS.frm can be deleted since
> it currently isn't used. The other arguments can be set as:
> delta.0.Y.0 = 50
> gamma = 40.
> cheers,
> dave
>
>
>
>
> -----Original Message-----
> From: Martin Henry H. Stevens [mailto:HStevens at MUOhio.edu]
> Sent: Wednesday, June 14, 2006 1:23 PM
> To: Afshartous, David
> Cc: r-help at stat.math.ethz.ch
> Subject: Re: [R] appending
>
> Hi David,
> It would be helpful if you supply a little data, upon which this would
> operate.
> Hank
> On Jun 14, 2006, at 12:31 PM, Afshartous, David wrote:
>
>> All,
>>
>> In the function below I have 24 individuals and 6 calculations per
>> individual.
>> The 6 calculations are collected each time in a 1:24 loop when
>> calculating "delta".
>>
>> I'd like to collect all 144 = 24*6 calculations in one vector
>> ("delta.patient.comb").
>> The function works as is via indexing, but is there an easier way to
>> collect the measurements via appendinng the 6 measurements each time
>> to the current set? I couldn't find anything in Intro to R on
>> appending.
>>
>> cheers,
>> Dave
>> ps - please respond directly to afshar at miami.edu
>>
>>
>>
>> creatine.function.new = function(delta.0.Y.0, gamma, comp.LIS.frm,
>> comp.CAND.frm) { ## function to calcuate the delta.i, i.e. the
>> percent
>
>> ## leftover ## gamma = rate of Cr going into bucket, e.g., mg/hr ##
>> delta.0.Y.0 = product of delta.0 and Y.0 at baseline ##
>> Y.1 = delta.0.Y.0 + gamma
>> delta = numeric(6)
>> delta.patient = numeric(24)
>> delta.patient.comb = numeric(144)
>> ##
>> for (k in 1:24) { ## each patient
>> patient.k.CAND = which(comp.CAND.frm$Patient_no == k)
>> Ucr.CAND.patient.k = comp.CAND.frm$Ucr[patient.k.CAND]
>> C = Ucr.CAND.patient.k ## 6 observed creatanine values
> for each
>> patient
>> delta[1] = (Y.1 - C[1])/Y.1
>> Y.i = Y.1
>> delta.i = delta[1]
>> for (i in 1:5) { ## six measurments per
>> patient
>> Y.i.plus.1 = delta.i * Y.i + gamma
>> delta.i.plus.1 = (Y.i.plus.1 -
>> C[i+1])/Y.i.plus.1
>> delta[i+1] = delta.i.plus.1
>> delta.i = delta[i+1]
>> Y.i = Y.i.plus.1
>> }
>> delta.patient[k] = list(delta)
>> delta.patient.comb[(6*(k-1)+1):(6*(k-1)+ 6)] = delta
>> }
>> list(delta.patient, delta.patient.comb) }
>>
>> ______________________________________________
>> R-help at stat.math.ethz.ch mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide! http://www.R-project.org/posting-
>> guide.html
>
> Dr. M. Hank H. Stevens, Assistant Professor
> 338 Pearson Hall
> Botany Department
> Miami University
> Oxford, OH 45056
>
> Office: (513) 529-4206
> Lab: (513) 529-4262
> FAX: (513) 529-4243
> http://www.cas.muohio.edu/~stevenmh/
> http://www.muohio.edu/ecology/
> http://www.muohio.edu/botany/
> "E Pluribus Unum"
>
>
>
>
> <comp.CAND.frm.frm>
Dr. M. Hank H. Stevens, Assistant Professor
338 Pearson Hall
Botany Department
Miami University
Oxford, OH 45056
Office: (513) 529-4206
Lab: (513) 529-4262
FAX: (513) 529-4243
http://www.cas.muohio.edu/~stevenmh/
http://www.muohio.edu/ecology/
http://www.muohio.edu/botany/
"E Pluribus Unum"
More information about the R-help
mailing list