I have a data.frame of entries. I also have a data.frame for each week that have associated counts for the entries. No such week count data.frame has every entry in it though, so the original list is a superset for each of them.
What I'd like to do is combine these so that I have a data.frame where the first column is the entry and the next N columns are the N week counts where if an entry does not have a count for that week, then it is considered 0.
My first attempt looked like this:
append_week_counts_to_entries <- function(entries) {
entries$week1 <- apply(entries,1,helpfunc,row=row,week=count_week1)
entries$week2 <- apply(entries,1,helpfunc,row=row,week=count_week2)
# ... to all N weeks
return(entries)
}
helpfunc <- function(entries,row,week) {
if(as.character(row[1]) %in% week$id) {
return(week[which(as.character(week$id) == as.character(row[1])),2])
}
else {
return(0)
}
}
(This worked until I abstracted it to how it looks now. I'd rather learn how it could work than keep the poor way of writing it I had before)
Besides not working as is, I also have a feeling that this is very inefficient for R. Help on both fronts would be much appreciated.
Edit: An example dataset would be:
entries: structure(list(`entries$id` = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10
)), .Names = "entries$id", row.names = c(NA, -10L), class = "data.frame")
count_week_i: structure(list(Var1 = structure(1:3, .Label = c("1", "2", "3"
), class = "factor"), Freq = c(1L, 2L, 4L)), .Names = c("Var1",
"Freq"), row.names = c(NA, -3L), class = "data.frame")