Thank you for that simple answer. Really appreciate it.
From: PIKAL Petr <petr.pi...@precheza.cz> To: Pavneet Arora/UK/RoyalSun@RoyalSun, "r-help@r-project.org" <r-help@r-project.org> Date: 28/07/2014 10:26 Subject: RE: [R] Differencing between 2 previous values Hi see in line > -----Original Message----- > From: r-help-boun...@r-project.org [mailto:r-help-bounces@r- > project.org] On Behalf Of Pavneet Arora > Sent: Monday, July 28, 2014 11:08 AM > To: r-help@r-project.org > Subject: [R] Differencing between 2 previous values > > Hello All, > > I am trying to do a simple thing of calculating the absolute difference > between 2 previous values. Since my original data consists of 30 rows, > this column where I am storing my absolute difference values only > consists of 29 rows (called the ?differ?)! And I am having troubling > cbind ing the > 2 columns. Is there any way I can make the first row of ?differ? > column as NA? > > So my data looks like following > dput(data) > tructure(list(week = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, > 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30), value > = c(9.45, 7.99, 9.29, 11.66, 12.16, 10.18, 8.04, 11.46, 9.2, 10.34, > 9.03, 11.47, 10.51, 9.4, 10.08, 9.37, 10.62, 10.31, 10, 13, 10.9, 9.33, > 12.29, 11.5, 10.6, 11.08, 10.38, 11.62, 11.31, 10.52)), .Names = > c("week", "value"), row.names = c(NA, -30L), class = "data.frame") > > This is how I calculate my ?diff? column: > differ <- abs(diff(data$value)) differ <- c(NA, abs(diff(data$value))) Regards Petr > Which gives me the following results: > [1] 1.46 1.30 2.37 0.50 1.98 2.14 3.42 2.26 1.14 1.31 2.44 0.96 [13] > 1.11 0.68 0.71 1.25 0.31 0.31 3.00 2.10 1.57 2.96 0.79 0.90 [25] 0.48 > 0.70 1.24 0.31 0.79 > > As you can see this only contains 29 rows, so when I try to cbind it to > my current data, I have an error. > cbind(differ,data) > Error in data.frame(..., check.names = FALSE) : > arguments imply differing number of rows: 29, 30 > > What I ideally want is my new dataset to look as: > Week Value Differ > 1 9.45 NA > 2 7.99 1.46 > 3 9.29 1.30 > > And so on?. > > *********************************************************************** > *********************************************************************** > *********************************************************************** > ** > MORE TH>N is a trading style of Royal & Sun Alliance Insurance plc (No. > 93792). Registered in England and Wales at St. Markâ??s Court, Chart > Way, Horsham, West Sussex, RH12 1XL. > > Authorised by the Prudential Regulation Authority and regulated by the > Financial Conduct Authority and the Prudential Regulation Authority. > *********************************************************************** > *********************************************************************** > *********************************************************************** > *** > > [[alternative HTML version deleted]] ________________________________ 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. ______________________________________________________________________________________________ The following message has been automatically added to comply with the RSA Group IT Security requirements: This email have arrived via the Internet without any form of encryption or verification of the sender. As such you should be especially cautious about its origin and content. Replies which contain sensitive information or legal/contractual obligations are particularly vulnerable. In these cases you should not reply unless you are authorised to do so, and adequate encryption is employed. If you have any questions, please speak to the Service Centre on x7979. ______________________________________________________________________________________________ *********************************************************************************************************************************************************************************************************************** MORE TH>N is a trading style of Royal & Sun Alliance Insurance plc (No. 93792). Registered in England and Wales at St. Markâs Court, Chart Way, Horsham, West Sussex, RH12 1XL. Authorised by the Prudential Regulation Authority and regulated by the Financial Conduct Authority and the Prudential Regulation Authority. ************************************************************************************************************************************************************************************************************************ [[alternative HTML version deleted]]
______________________________________________ R-help@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.