data = data.frame(
focal = c("John","John","John","John","Albert","Charles","Charles","Jay","Jay","Jay"),
type = c("Baseline","Baseline","Baseline","Baseline","Experimental","Experimental","Experimental","Baseline","Baseline","Baseline"),
partner5M = c("Martin","Albert","Chris","Chris","John","Albert","Rich","Martin","Albert","Alfred"),
duration = sample(c(1:50),10),
header = TRUE
)
I need to extract data from my data frame as follows (it works) :
Xindiv = database[ which ( data$focal == "John" & data$type == "Baseline" & database$partner5M == "indiv"),4]
Xindiv = unlist(Xindiv,use.names = FALSE)
Xindiv = chron(times = Xindiv)
The problem is that I need to do that for a lot of individuals, and I also need to be able to quickly change the conditions in the which.
So what I would like to do is a for loop which would roughly be as follows :
indiv = c("indiv1","indiv2","indiv3") #every different individuals in partner5M
for(indiv in length(indiv)) {
Xindiv = database[ which ( database$Focal == "JohnDoe" & database$Type == "Baseline" & database$Partner5m == "indiv"),24]
Xindiv = unlist(Xindiv,use.names = FALSE)
Xindiv = chron(times = Xindiv)
}
I would like the outcome of this to be :
Xindiv1 = ...
Xindiv2 = ...
Xindiv3 = ...
and so on, but I have no clue as to how to make such a loop, so I would be extremely grateful for any advices on the method.
Cheers, Max