[R] Loop does not work: Error in else statement
PIKAL Petr
petr.pikal at precheza.cz
Tue Sep 30 08:46:43 CEST 2014
Hi
> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of Frank S.
> Sent: Monday, September 29, 2014 9:17 PM
> To: r-help at r-project.org
> Subject: [R] Loop does not work: Error in else statement
>
> Hi to all members of R list,
>
>
>
> I m working with data.table package, and with 6
> variables: "ID" (Identifier), "born" (Birthdate), "start" (Starting
> date), "register" (date of measurement), "value"and "end" (date of
> expiration). So, the natural order of dates would be: born =< start =<
> register =< end. As an example, I have: DT <- data.table(ID =
> as.factor(rep(1:4,each=2)),
>
> born =
> as.Date(rep(c("1955-02-20", "1990-07-25", "1972-03-18", "1988-05-
> 03"),each=2)),
>
> start =
> as.Date(rep(c("1953-03-28", "1990-07-01", "1983-09-05", "1988-07-
> 18"),each=2)),
>
> register =
> as.Date(c("1955-08-11", "1958-03-28",
> "1990-07-09", "1992-07-01",
>
> "1983-09-05",
> "2002-09-28", "1992-07-10", "1993-03-12")),
>
> value =
> c(205, 346, 34, 76, 320, 148, 209, 274),
>
> end =
> as.Date(rep(c("1960-11-05", "1997-10-15", "2002-09-27", "1997-03-
> 02"),each=2)))
>
>
>
> I would want to make 3 operations:1. First: Remove entire ID s where
> "start" is previous to "born" date (excepting those subjects whose
> month and year values are the same in "start" and "born" variables: I
> assign "born"
> date to "start" date in these cases).Afterwords:2. Remove only specific
> rows (not all ID) where register is previous to start .3.Remove only
> specific rows (not all ID) where end is previous to register .
>
> I have: DT[ , { if (all(born > start)) { indx <-
> which(paste(year(born)) == paste(year(start)) & paste(month(born)) ==
> paste(month(start))) result <- list(born=born[indx],
> start=born[indx], register=register[indx], value=value[indx],
> end=end[indx]) } if (all(register > start) | all(end >
> register)) { indx <- which((register > start) | (end >
> register)) result <- list(born=born[indx], start=start[indx],
> register=register[indx], value=value[indx], end=end[indx]) }
> else { NULL } else { indx <-
> which(all(register > start) | all(end > register)) result <-
> list(born=born[indx], start=start[indx], register=register[indx],
> value=value[indx], end=end[indx]) } result }, by=ID] BUT I GET
> AN ERROR MESSAGE: Error: syntax error, unexpected ELSE in "else"
> Please, can anyone help me? Thank you!!
Missing parentheses? Missing comma? Syntactically valid expression before else?
From HTML scrambled post hard to tell.
Wild guess.
some if statement could be evaluated without before else statement as valid.
if (something) {do this}
else {do that}
results in error as first line is evaluated and else is not needed for this evaluation. Therefore you start the second line without leading if.
if (something) {do this} else
{do that}
is valid and does not throw error.
Maybe it is covered in FAQs
Regards
Petr
>
>
> [[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.
More information about the R-help
mailing list