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]]
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.
Petr, Your code works therefore I pronounce it beautiful. Many many thanks --Seth -----Original Message----- From: PIKAL Petr [mailto:petr.pikal at precheza.cz] Sent: Tuesday, February 10, 2015 11:23 AM To: Seth Bigelow; r-help at r-project.org Subject: RE: [R] function that calculates using preceding records 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.
Hi, Here is an implementation:> data <- read.table("tree.txt",header=TRUE,sep=",",stringsAsFactors=FALSE) > datatreecode year rw d 1 TC149 2014 NA 8 2 TC149 2013 0.080 NA 3 TC149 2012 0.125 NA 4 TC149 2011 0.120 NA 5 TC149 2010 0.125 NA 6 TC148 2014 NA 34 7 TC148 2013 0.300 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.260 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.750 NA 18 TC146 2012 1.810 NA 19 TC146 2011 1.390 NA 20 TC146 2010 1.940 NA> calDiameter <- function(x)+ { + temp <- x + for (i in 1:dim(temp)[1]) + { + if (dim(x[x$treecode==temp$treecode[i] & x$year == temp$year[i] - 1 & !is.na(x$rw),])[1] == 1 & !is.na(temp$d[i])) + { + temp$d[i] <- as.numeric(temp$d[i]) - as.numeric(x[x$treecode =temp$treecode[i] & x$year == temp$year[i] - 1 & !is.na(x$rw), ]$rw[1]) + } + } + return(temp) + }> calDiameter(data)treecode year rw d 1 TC149 2014 NA 7.92 2 TC149 2013 0.080 <NA> 3 TC149 2012 0.125 <NA> 4 TC149 2011 0.120 <NA> 5 TC149 2010 0.125 NA 6 TC148 2014 NA 33.7 7 TC148 2013 0.300 <NA> 8 TC148 2012 0.335 <NA> 9 TC148 2011 0.315 <NA> 10 TC148 2010 0.455 NA 11 TC147 2014 NA 54.24 12 TC147 2013 1.260 <NA> 13 TC147 2012 1.115 <NA> 14 TC147 2011 1.025 <NA> 15 TC147 2010 1.495 NA 16 TC146 2014 NA 58.25 17 TC146 2013 1.750 <NA> 18 TC146 2012 1.810 <NA> 19 TC146 2011 1.390 <NA> 20 TC146 2010 1.940 NA There were 12 warnings (use warnings() to see them) -- View this message in context: http://r.789695.n4.nabble.com/function-that-calculates-using-preceding-records-tp4703024p4703069.html Sent from the R help mailing list archive at Nabble.com.