I have a variable called "exposed" and I know already the sum of exposed people over time: have a look to understand
| i | exposed |
|---|---|
| 1 | y |
| 2 | y |
| 3 | y |
| 4 | n |
| 5 | n |
So I have 3 exposed individuals and 2 are not.
t <- 5
#I know that each i in t :
sum(exposed[i]) <- c(3,4,1,4,5)
I created this line of code to cupture the change in data:
evol <- list()
for(i in 1:t){evol[[i]]<- df}
for (i in 2:t) {
# condition
}
My question is : what is the condition that I have to write to have in:
evol[[1]]
a data that looks like this:
| i | exposed|
|:—-:|:——:|
| 1 | y |
| 2 | y
|3 | y
|4 | n
|5 | n
evol[[2]]
the data that looks like this:
| i | exposed|
|:—-:|:——:|
| 1 | y |
| 2 | y
|3 | y
|4 | y
|5 | n
evol[[3]]
a data that looks like this:
| i | exposed|
|:—-:|:——:|
| 1 | y |
| 2 | n
|3 | n
|4 | n
|5 | n
I hope I made it clear;
Any ideas pleas;
kind regards.
>Solution :
If I’m understanding you correctly, you want a list of dataframes based on the exposed sums.
Using lapply you can do
exposed <- c(3,4,1,4,5)
evol <- lapply(exposed, \(x) data.frame(i = seq_along(exposed), exposed = c(rep("y", x), rep("n", length(exposed) - x ))) )
evol[[1]]
i exposed
1 1 y
2 2 y
3 3 y
4 4 n
5 5 n