Dear R users. I am little lost and i need your help. I have such data. DATE i Symptomes t 1 2009-04-24 Mexique 0 14358 2 2009-04-24 usa 0 14358 3 2009-04-26 Mexique 18 14360 4 2009-04-26 usa 100 14360 5 2009-04-27 Canada 6 14361 6 2009-04-27 Mexique 26 14361 7 2009-04-27 Spain 1 14361 8 2009-04-27 usa 40 14361 9 2009-04-28 Canada 6 14362 10 2009-04-28 Israel 2 14362 11 2009-04-28 Mexique 26 14362 12 2009-04-28 New Zealand 3 14362 13 2009-04-28 Spain 2 14362 14 2009-04-28 United Kingdom 2 14362 15 2009-04-28 usa 64 14362 16 2009-04-29 Canada 13 14363 17 2009-04-29 Austria 1 14363 18 2009-04-29 Germany 3 14363 19 2009-04-29 Israel 2 14363 20 2009-04-29 Mexique 26 14363 21 2009-04-29 New Zealand 3 14363 22 2009-04-29 SPAIN 4 14363 23 2009-04-29 United Kingdom 5 14363 A data with a date in character format,i wich reprent a country,Symptomes wich is a number of subjects having a disease,t the date convert in numeric. I want to create two other variables (for example INCUBATION and CONTAGIEUX) Incubation[ i ] add values for symptomes for the two previous days (for t<=t-1) CONTAGIEUX [ i ] add values for symptomes for the 7 next days (for t>=t+6). I want the two variables INCUBATION and CONTAGIEUX to be cumulative for next and previous date. For example for date i Symptomes Incubation CONTAGIEUX 2009-04-24 Mexique 0 18+0 0 2009-04-26 Mexique 18 18+26 18+0 2009-04-27 MEXIQUE 26 18+26+... 18+26... Thanks. -- View this message in context: http://r.789695.n4.nabble.com/Help-tp4650647.html Sent from the R help mailing list archive at Nabble.com.
Hello,
Try the following.
incub <- function(x){
x$Incubation <- 0
x$Incubation[1] <- x$Symptomes[1]
if(nr > 1)
x$Incubation[2] <- sum(x$Symptomes[1:2])
for(i in seq_len(nrow(x))[-(1:2)])
x$Incubation[i] <- sum(x$Symptomes[i - (0:2)])
x
}
contag <- function(x){
x$CONTAGIEUX <- 0
for(i in seq_len(nrow(x)))
x$CONTAGIEUX[i] <- sum(x$Symptomes[i + 0:6], na.rm = TRUE)
x
}
result <- lapply(split(dat, dat$i), incub)
result <- lapply(result, contag)
result <- do.call(rbind, result)
rownames(result) <- seq_len(nrow(result))
result
Hope this helps,
Rui Barradas
Em 24-11-2012 14:39, anoumou escreveu:> Dear R users.
> I am little lost and i need your help.
> I have such data.
> DATE i Symptomes t
> 1 2009-04-24 Mexique 0 14358
> 2 2009-04-24 usa 0 14358
> 3 2009-04-26 Mexique 18 14360
> 4 2009-04-26 usa 100 14360
> 5 2009-04-27 Canada 6 14361
> 6 2009-04-27 Mexique 26 14361
> 7 2009-04-27 Spain 1 14361
> 8 2009-04-27 usa 40 14361
> 9 2009-04-28 Canada 6 14362
> 10 2009-04-28 Israel 2 14362
> 11 2009-04-28 Mexique 26 14362
> 12 2009-04-28 New Zealand 3 14362
> 13 2009-04-28 Spain 2 14362
> 14 2009-04-28 United Kingdom 2 14362
> 15 2009-04-28 usa 64 14362
> 16 2009-04-29 Canada 13 14363
> 17 2009-04-29 Austria 1 14363
> 18 2009-04-29 Germany 3 14363
> 19 2009-04-29 Israel 2 14363
> 20 2009-04-29 Mexique 26 14363
> 21 2009-04-29 New Zealand 3 14363
> 22 2009-04-29 SPAIN 4 14363
> 23 2009-04-29 United Kingdom 5 14363
> A data with a date in character format,i wich reprent a country,Symptomes
> wich is a number of subjects having a disease,t the date convert in
numeric.
> I want to create two other variables (for example INCUBATION and
CONTAGIEUX)
> Incubation[ i ] add values for symptomes for the two previous days (for
> t<=t-1)
>
> CONTAGIEUX [ i ] add values for symptomes for the 7 next days (for
t>=t+6).
> I want the two variables INCUBATION and CONTAGIEUX to be cumulative for
next
> and previous date.
>
> For example for
>
> date i Symptomes Incubation CONTAGIEUX
> 2009-04-24 Mexique 0 18+0 0
>
> 2009-04-26 Mexique 18 18+26 18+0
>
> 2009-04-27 MEXIQUE 26 18+26+... 18+26...
> Thanks.
>
>
>
>
> --
> View this message in context:
http://r.789695.n4.nabble.com/Help-tp4650647.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at 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.
Hello,
Right, sorry, it should be nrow(x). I had created a variable nr <-
nrow(x) and forgot to check it after changing it.
incub <- function(x){
x$Incubation <- 0
x$Incubation[1] <- x$Symptomes[1]
if(nrow(x) > 1)
x$Incubation[2] <- sum(x$Symptomes[1:2])
for(i in seq_len(nrow(x))[-(1:2)])
x$Incubation[i] <- sum(x$Symptomes[i - (0:2)])
x
}
Now it works.
Rui Barradas
Em 24-11-2012 18:18, arun escreveu:> HI Rui,
> Seems like nr is not defined.
> lapply(split(dat1, dat1$i), incub)
> #Error in FUN(X[[1L]], ...) : object 'nr' not found
> A.K.
>
>
>
>
> ----- Original Message -----
> From: Rui Barradas <ruipbarradas at sapo.pt>
> To: anoumou <teko_maurice at yahoo.fr>
> Cc: r-help at r-project.org
> Sent: Saturday, November 24, 2012 12:47 PM
> Subject: Re: [R] Help!!!!!
>
> Hello,
> Try the following.
>
> incub <- function(x){
> x$Incubation <- 0
> x$Incubation[1] <- x$Symptomes[1]
> if(nr > 1)
> x$Incubation[2] <- sum(x$Symptomes[1:2])
> for(i in seq_len(nrow(x))[-(1:2)])
> x$Incubation[i] <- sum(x$Symptomes[i - (0:2)])
> x
> }
> contag <- function(x){
> x$CONTAGIEUX <- 0
> for(i in seq_len(nrow(x)))
> x$CONTAGIEUX[i] <- sum(x$Symptomes[i + 0:6], na.rm = TRUE)
> x
> }
> result <- lapply(split(dat, dat$i), incub)
> result <- lapply(result, contag)
> result <- do.call(rbind, result)
> rownames(result) <- seq_len(nrow(result))
> result
>
>
> Hope this helps,
>
> Rui Barradas
> Em 24-11-2012 14:39, anoumou escreveu:
>> Dear R users.
>> I am little lost and i need your help.
>> I have such data.
>> DATE i Symptomes t
>> 1 2009-04-24 Mexique 0 14358
>> 2 2009-04-24 usa 0 14358
>> 3 2009-04-26 Mexique 18 14360
>> 4 2009-04-26 usa 100 14360
>> 5 2009-04-27 Canada 6 14361
>> 6 2009-04-27 Mexique 26 14361
>> 7 2009-04-27 Spain 1 14361
>> 8 2009-04-27 usa 40 14361
>> 9 2009-04-28 Canada 6 14362
>> 10 2009-04-28 Israel 2 14362
>> 11 2009-04-28 Mexique 26 14362
>> 12 2009-04-28 New Zealand 3 14362
>> 13 2009-04-28 Spain 2 14362
>> 14 2009-04-28 United Kingdom 2 14362
>> 15 2009-04-28 usa 64 14362
>> 16 2009-04-29 Canada 13 14363
>> 17 2009-04-29 Austria 1 14363
>> 18 2009-04-29 Germany 3 14363
>> 19 2009-04-29 Israel 2 14363
>> 20 2009-04-29 Mexique 26 14363
>> 21 2009-04-29 New Zealand 3 14363
>> 22 2009-04-29 SPAIN 4 14363
>> 23 2009-04-29 United Kingdom 5 14363
>> A data with a date in character format,i wich reprent a
country,Symptomes
>> wich is a number of subjects having a disease,t the date convert in
numeric.
>> I want to create two other variables (for example INCUBATION and
CONTAGIEUX)
>> Incubation[ i ] add values for symptomes for the two previous days (for
>> t<=t-1)
>>
>> CONTAGIEUX [ i ] add values for symptomes for the 7 next days (for
t>=t+6).
>> I want the two variables INCUBATION and CONTAGIEUX to be cumulative for
next
>> and previous date.
>>
>> For example for
>>
>> date i Symptomes Incubation CONTAGIEUX
>> 2009-04-24 Mexique 0 18+0 0
>>
>> 2009-04-26 Mexique 18 18+26 18+0
>>
>> 2009-04-27 MEXIQUE 26 18+26+... 18+26...
>> Thanks.
>>
>>
>>
>>
>> --
>> View this message in context:
http://r.789695.n4.nabble.com/Help-tp4650647.html
>> Sent from the R help mailing list archive at Nabble.com.
>>
>> ______________________________________________
>> R-help at 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.
> ______________________________________________
> R-help at 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.
>
Hello, Thank you very much for your help. I appreciate. :-) I want the count of INCUBATION and CONTAGIEUX to be by country. the count for example of mexique must not be the same as for USA. Have u an idea? Thanks you in advance. anoumou -- View this message in context: http://r.789695.n4.nabble.com/Help-tp4650647p4650705.html Sent from the R help mailing list archive at Nabble.com.