Hi

Slightly better but still html scrambled.

see in line

> -----Original Message-----
> From: r-help-boun...@r-project.org [mailto:r-help-bounces@r-
> project.org] On Behalf Of Frank S.
> Sent: Tuesday, September 30, 2014 2:55 PM
> To: r-help@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.
______________________________________________
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