Hi
Slightly better but still html scrambled.
see in line
> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of Frank S.
> Sent: Tuesday, September 30, 2014 2:55 PM
> To: r-help at r-project.org
> Subject: [R] Loop does not work: Error in else statement (II)
>
>
>
> Hi to all members of R list,
>
>
>
> I m working with data.table package, and with 6
> variables:
>
>
>
> ID: Subject identifier
>
> born: Birthdate
>
> start: Starting date
>
> register: date of measurement
>
> value: Value of measurement
>
> end: date of expiration of the measurements.
>
>
>
> So, the natural order of date variables 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:
>
> a) 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).
> DT$start-DT$born
Time differences in days
[1] -694 -694 -24 -24 4188 4188 76 76
suggests you can remove line 1 and 2 and check line 3 and 4
as.numeric(format(DT$start, "%Y.%m"))- as.numeric(format(DT$born,
"%Y.%m"))
[1] -1.99 -1.99 0.00 0.00 11.06 11.06 0.02 0.02
so only lines 1 and 2 shall be removed
#first change born to start where appropriate
cond1 <- which((as.numeric(format(DT$start, "%Y.%m"))-
as.numeric(format(DT$born, "%Y.%m")))==0)
DT$born[cond1] <- DT$start[cond1]
#then select rows for which start is earlier than born
cond2 <-((DT$start-DT$born)<0)
#remove rows
DT2<-DT[!cond2,]
If there are rows which does not hold for some ID it is trickier.
Hint
DT$ID[cond2]
>
> b) Remove
> only specific rows (not all ID) where register is previous to start
#select rows which hold your condition
cond3 <-((DT2$register-DT2$born)<0)
#no items does not result as TRUE but
#remove rows which are TRUE in cond3
DT2 <-DT2[!cond3,]
> .
>
> c) Remove
> only specific rows (not all ID) where end is previous to register .
>
the same principle applies here. You shall be able to do it yourself
No if, no else no loop, no error.
If there are NAs the solution would be slightly different but in principle
similar.
Regards
Petr
>
>
> 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]
>
>
>
> Error: unexpected 'else' in:
>
> " }
>
> else"
>
>
>
>
> [[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.