[R] How to adjust the start of a series to zero? (i.e. subtract the first value from the sequence)

David Winsemius dwinsemius at comcast.net
Mon Jul 2 21:25:43 CEST 2012


On Jul 2, 2012, at 2:21 AM, Bert Gunter wrote:

> Hi Kristina:
>
> That's because I gave you the wrong function. It should be "with" not
> "within", as:
>
> sort2v4$adj_mean<-with(sort2v4, ave(mean, point, FUN=function(x)x- 
> x[1]))
>
> within(dat,...) returns dat suitably modified.
> with (dat,...) just returns the result of the function.
>
> I frequently use within() when I mean with(), unfortunately.  Sorry  
> for the
> error.
>

That's interesting to me. I almost never use `within`. I'm wondering  
if I'm missing something? As I read the help page `within` is going to  
do something different when the expression is an assignment, so one  
could do the following with `within`

within(dfrm, var2 <- var2/100)

... but that would have no lasting effect since its result was not  
assigned, so I do not se an advantage over:

  dfrm$var2 <- with(dfrm, var2/100)

In both case one would still need to assign the result to an object,  
in the case of the first to 'dfrm' itself and in the second case to  
dfrm[['var2']].

None of the examples on the help page use `within`. Is that because it  
is inferior to transform?



> -- Bert
>
> On Sun, Jul 1, 2012 at 6:39 PM, Kristiina Hurme
> <kristiina.hurme at uconn.edu>wrote:
>
>> Thanks everyone.
>> I tried them all, and got all to work except for the last one.
>>
>> I tried
>>> sort2v4$adj_mean<-within(sort2v4, ave(mean, point,
>> FUN=function(x)x-x[1]))
>>> head(sort2v4)
>>  point time     mean        sd adj_mean.point adj_mean.time  
>> adj_mean.mean
>> adj_mean.sd
>> 1     1    1 52.50100 1.5073927              1             1       
>> 52.50100
>> 1.5073927
>> 3     1    2 54.50182 0.8510329              1             2       
>> 54.50182
>> 0.8510329
>> 4     1    3 56.60174 1.5787222              1             3       
>> 56.60174
>> 1.5787222
>> 5     1    4 57.20000 1.2292726              1             4       
>> 57.20000
>> 1.2292726
>> 6     1    5 59.30000 2.2632327              1             5       
>> 59.30000
>> 2.2632327
>> 7     1    6 57.80089 1.4745218              1             6       
>> 57.80089
>> 1.4745218
>>
>> but am getting the columns in duplicate, rather than it performing  
>> the
>> function. Any advice?
>>
>> Thanks again,
>> Kristiina
>>
>> --
>> View this message in context:
>> http://r.789695.n4.nabble.com/How-to-adjust-the-start-of-a-series-to-zero-i-e-subtract-the-first-value-from-the-sequence-tp4634999p4635062.html
>> Sent from the R help mailing list archive at Nabble.com.
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide
>> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
>
>
>
> -- 
>
> Bert Gunter
> Genentech Nonclinical Biostatistics
>
> Internal Contact Info:
> Phone: 467-7374
> Website:
> http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list