[R] function that calculates using preceding records

PIKAL Petr petr.pikal at precheza.cz
Tue Feb 10 17:22:38 CET 2015


Hi

I found an extremely ugly code :-)

# first reverse levels of treecode to correspond with order of values in data frame

temp$treecode<-factor(temp$treecode, rev(levels(temp$treecode)))

# add zeroes and values to rw and d

temp$rw[is.na(temp$rw)]<-0
library(zoo)
temp$d<-na.locf(temp$d)

# split your data acording to treecode

temp.l<-split(temp, temp$treecode)

# subtract from d cumulative sum of rw

mat<-sapply(sapply(temp.l, "[", 4), mean)-t(sapply(sapply(temp.l, "[",3), cumsum))

# transpose mat and remove dimension from mat
mat<-t(mat)
dim(mat)<-NULL

Now

temp <- cbind(temp, dsub=mat)

shall put subtracted values in correct order to your object.

Cheers
Petr


> -----Original Message-----
> From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Seth
> Bigelow
> Sent: Tuesday, February 10, 2015 3:30 PM
> To: r-help at r-project.org
> Subject: [R] function that calculates using preceding records
>
> Greetings:
>
>
>
> My dataframe has 4 variables: treecode, year, rw (tree ring width), and
> d (tree diameter). The d variable
>
> only has data for 2014. I wish to calculate earlier diameters by
> subtracting each year's growth (rw) from the
>
> previous year's diameter, by treecode. Can anyone help me with a
> function or statement that will do this?
>
> Sample dataset below: In this example, d in year 2013 for treecode
> TC149 would be 7.92 = 8.0 - 0.080.
>
>
>
> "treecode","year","rw","d"
>
> "1","TC149",2014,NA,8
>
> "2","TC149",2013,0.08,NA
>
> "3","TC149",2012,0.125,NA
>
> "4","TC149",2011,0.12,NA
>
> "5","TC149",2010,0.125,NA
>
> "6","TC148",2014,NA,34
>
> "7","TC148",2013,0.3,NA
>
> "8","TC148",2012,0.335,NA
>
> "9","TC148",2011,0.315,NA
>
> "10","TC148",2010,0.455,NA
>
> "11","TC147",2014,NA,55.5
>
> "12","TC147",2013,1.26,NA
>
> "13","TC147",2012,1.115,NA
>
> "14","TC147",2011,1.025,NA
>
> "15","TC147",2010,1.495,NA
>
> "16","TC146",2014,NA,60
>
> "17","TC146",2013,1.75,NA
>
> "18","TC146",2012,1.81,NA
>
> "19","TC146",2011,1.39,NA
>
> "20","TC146",2010,1.94,NA
>
>
>
>
>
> Seth W. Bigelow
>
>
>       [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.

________________________________
Tento e-mail a jakékoliv k němu připojené dokumenty jsou důvěrné a jsou určeny pouze jeho adresátům.
Jestliže jste obdržel(a) tento e-mail omylem, informujte laskavě neprodleně jeho odesílatele. Obsah tohoto emailu i s přílohami a jeho kopie vymažte ze svého systému.
Nejste-li zamýšleným adresátem tohoto emailu, nejste oprávněni tento email jakkoliv užívat, rozšiřovat, kopírovat či zveřejňovat.
Odesílatel e-mailu neodpovídá za eventuální škodu způsobenou modifikacemi či zpožděním přenosu e-mailu.

V případě, že je tento e-mail součástí obchodního jednání:
- vyhrazuje si odesílatel právo ukončit kdykoliv jednání o uzavření smlouvy, a to z jakéhokoliv důvodu i bez uvedení důvodu.
- a obsahuje-li nabídku, je adresát oprávněn nabídku bezodkladně přijmout; Odesílatel tohoto e-mailu (nabídky) vylučuje přijetí nabídky ze strany příjemce s dodatkem či odchylkou.
- trvá odesílatel na tom, že příslušná smlouva je uzavřena teprve výslovným dosažením shody na všech jejích náležitostech.
- odesílatel tohoto emailu informuje, že není oprávněn uzavírat za společnost žádné smlouvy s výjimkou případů, kdy k tomu byl písemně zmocněn nebo písemně pověřen a takové pověření nebo plná moc byly adresátovi tohoto emailu případně osobě, kterou adresát zastupuje, předloženy nebo jejich existence je adresátovi či osobě jím zastoupené známá.

This e-mail and any documents attached to it may be confidential and are intended only for its intended recipients.
If you received this e-mail by mistake, please immediately inform its sender. Delete the contents of this e-mail with all attachments and its copies from your system.
If you are not the intended recipient of this e-mail, you are not authorized to use, disseminate, copy or disclose this e-mail in any manner.
The sender of this e-mail shall not be liable for any possible damage caused by modifications of the e-mail or by delay with transfer of the email.

In case that this e-mail forms part of business dealings:
- the sender reserves the right to end negotiations about entering into a contract in any time, for any reason, and without stating any reasoning.
- if the e-mail contains an offer, the recipient is entitled to immediately accept such offer; The sender of this e-mail (offer) excludes any acceptance of the offer on the part of the recipient containing any amendment or variation.
- the sender insists on that the respective contract is concluded only upon an express mutual agreement on all its aspects.
- the sender of this e-mail informs that he/she is not authorized to enter into any contracts on behalf of the company except for cases in which he/she is expressly authorized to do so in writing, and such authorization or power of attorney is submitted to the recipient or the person represented by the recipient, or the existence of such authorization is known to the recipient of the person represented by the recipient.


More information about the R-help mailing list