Guest User

Untitled

a guest
Aug 14th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.78 KB | None | 0 0
  1. Apply With Multiple Arguments
  2. append_week_counts_to_entries <- function(entries) {
  3. entries$week1 <- apply(entries,1,helpfunc,row=row,week=count_week1)
  4. entries$week2 <- apply(entries,1,helpfunc,row=row,week=count_week2)
  5. # ... to all N weeks
  6. return(entries)
  7. }
  8.  
  9. helpfunc <- function(entries,row,week) {
  10. if(as.character(row[1]) %in% week$id) {
  11. return(week[which(as.character(week$id) == as.character(row[1])),2])
  12. }
  13. else {
  14. return(0)
  15. }
  16. }
  17.  
  18. entries: structure(list(`entries$id` = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10
  19. )), .Names = "entries$id", row.names = c(NA, -10L), class = "data.frame")
  20.  
  21. count_week_i: structure(list(Var1 = structure(1:3, .Label = c("1", "2", "3"
  22. ), class = "factor"), Freq = c(1L, 2L, 4L)), .Names = c("Var1",
  23. "Freq"), row.names = c(NA, -3L), class = "data.frame")
Add Comment
Please, Sign In to add comment