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