celestialgod

data aggregation

Oct 5th, 2015
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 3.69 KB | None | 0 0
  1. number=50
  2. data <- matrix(nrow = number, ncol = 3 )
  3. colnames(data) <- c("ID",  "Shop_year", "Region")
  4. set.seed(1)
  5. data[,1] <- c(sample(1:25, size=number, replace = T) )#ID
  6.  
  7. data[,2] <- c(sample(c("year2013","year2014","year2015"), size=number, replace= T, prob = c(0.1, 0.3, 0.6) ) )
  8. data[,3] <- c(sample(c("北區一","北區二","北區三"), size = number,
  9.                      replace = T, prob = c(0.5,0.3,0.2) ))
  10. data <- data.frame(data)
  11.  
  12. library(reshape2)
  13. library(reshape)
  14. result <- cast(data, ID~Shop_year,value=c("Region"))
  15.  
  16. ##分類成三組
  17. data_new <- with(result, subset(result, year2015 !=0 & year2014 == 0))
  18. data_new$level <- "新增"
  19. data_lost <- with(result, subset(result, year2015 == 0))
  20. data_lost$level <- "流失"
  21. data_now <- with(result, subset(result, year2015 != 0 & year2014 !=0))
  22. data_now$level <- "回購"
  23.  
  24. ##合併檔案
  25. data_combine <- do.call(rbind, list(data_now, data_new, data_lost))
  26. ## 新增分組資料到原始檔案
  27. data_final <- merge(data, data_combine)
  28. #    ID Shop_year Region year2013 year2014 year2015 level
  29. # 1   1  year2014 北區二        0        1        1  回購
  30. # 2   1  year2015 北區一        0        1        1  回購
  31. # 3  10  year2014 北區一        0        2        3  回購
  32. # 4  10  year2015 北區一        0        2        3  回購
  33. # 5  10  year2014 北區一        0        2        3  回購
  34. # 6  10  year2015 北區一        0        2        3  回購
  35. # 7  10  year2015 北區一        0        2        3  回購
  36. # 8  11  year2015 北區二        0        0        1  新增
  37. # 9  12  year2015 北區二        0        0        1  新增
  38. # 10 13  year2015 北區二        0        0        3  新增
  39.  
  40. ## method 1
  41. library(dplyr)
  42. library(magrittr)
  43. library(reshape2)
  44. library(data.table)
  45. DT = data %>% tbl_dt() %>% mutate(ID = ID %>% as.character %>% as.integer)
  46. DT %>% dcast.data.table(ID ~ Shop_year) %>%
  47.   mutate(level = ifelse(year2014 !=0 && year2015 ==0, "流失", ifelse(
  48.     year2014 !=0 && year2015 !=0, "回購", "新增"
  49.   ))) %>% left_join(DT) %>% arrange(ID) %>% distinct %>%
  50.   select(-Shop_year)
  51.  
  52. #    ID year2013 year2014 year2015 level Region
  53. # 1   1        0        1        1  回購 北區二
  54. # 2   2        0        0        1  回購 北區二
  55. # 3   3        0        0        1  回購 北區二
  56. # 4   4        0        0        1  回購 北區一
  57. # 5   5        0        0        2  回購 北區二
  58. # 6   6        1        1        1  回購 北區二
  59. # 7   7        0        0        2  回購 北區二
  60. # 8   9        1        0        0  回購 北區二
  61. # 9  10        0        2        3  回購 北區一
  62. # 10 11        0        0        1  回購 北區二
  63.  
  64. ## method 2
  65. library(dplyr)
  66. library(tidyr)
  67. library(magrittr)
  68. library(data.table)
  69. DT = data %>% tbl_dt() %>% mutate(ID = ID %>% as.character %>% as.integer,
  70.   Shop_year = factor(Shop_year, levels = paste0("year", 2013:2015)))
  71. DT %>% gather(variable, years, -ID, -Region) %>% select(-variable) %>%
  72.   group_by(ID, years) %>%
  73.   mutate(count = length(Region)) %>% group_by(ID) %>%
  74.   mutate(level = ifelse(all(c("year2014", "year2015") %in% years), "回購",
  75.     ifelse("year2015" %in% years, "新增", "流失"))) %>% arrange(ID, years)
  76.  
  77. #    ID Region    years count level
  78. # 1   1 北區二 year2014     1  回購
  79. # 2   1 北區一 year2015     1  回購
  80. # 3   2 北區二 year2015     1  新增
  81. # 4   3 北區二 year2015     1  新增
  82. # 5   4 北區一 year2015     1  新增
  83. # 6   5 北區二 year2015     2  新增
  84. # 7   5 北區二 year2015     2  新增
  85. # 8   6 北區三 year2013     1  回購
  86. # 9   6 北區一 year2014     1  回購
  87. # 10  6 北區二 year2015     1  回購
  88. # .. ..    ...      ...   ...   ...
Advertisement
Add Comment
Please, Sign In to add comment