Guest User

Untitled

a guest
Jan 18th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.25 KB | None | 0 0
  1. require(dplyr)
  2. set.seed(1)
  3. large_df <- data_frame(id = rep(paste0('id',1:40), each = 3),
  4. age = c(rep(NA,60),rep (sample(20), each = 3)),
  5. col3 = rep(letters[1:20],6), col4 = rep(1:60,2))
  6. small_df <- data_frame(id = paste0('id',1:20),
  7. age = sample(20))
  8.  
  9. for(i in nrow(large_df)) {
  10. if (large_df[i,'id'] %in% small_df$id == TRUE) {
  11. large_df[i,'age'] <- small_df$age[which(small_df$id %in% large_df[i,'id'])]
  12. }
  13. }
  14.  
  15. large_df$age[1:60] <- rep(small_df$age, each = 3)
  16. large_df
  17. # A tibble: 120 x 4
  18. id age col3 col4
  19. <chr> <int> <chr> <int>
  20. 1 id1 6 a 1
  21. 2 id1 6 b 2
  22. 3 id1 6 c 3
  23. 4 id2 8 d 4
  24. 5 id2 8 e 5
  25. 6 id2 8 f 6
  26. 7 id3 11 g 7
  27. 8 id3 11 h 8
  28. 9 id3 11 i 9
  29. 10 id4 16 j 10
  30. # ... with 110 more rows
  31.  
  32. result =
  33. large_df %>%
  34. left_join(small_df, by = 'id') %>%
  35. mutate(age = ifelse(is.na(age.x), age.y, age.x)) %>%
  36. dplyr::select(-age.x, -age.y)
  37. result
  38. # A tibble: 120 x 4
  39. id col3 col4 age
  40. <chr> <chr> <int> <int>
  41. 1 id1 a 1 19
  42. 2 id1 b 2 19
  43. 3 id1 c 3 19
  44. 4 id2 d 4 5
Add Comment
Please, Sign In to add comment