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.
______________________________________________
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.

Reply via email to