Advertisement
celestialgod

SortDataFrame

Mar 22nd, 2015
286
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 1.34 KB | None | 0 0
  1. library(data.table)
  2. library(plyr)
  3. library(dplyr)
  4. library(magrittr)
  5.  
  6. ## data generation
  7. N = 10000
  8. dataset = sample(state.abb, N, replace = TRUE) %>%
  9.   tbl_dt() %>% setnames("state") %>%
  10.   mutate(hospital = paste0("Hospital", 1:N),
  11.     birthrate = runif(N, 0, 0.1), deathrate = runif(N, 0, 0.2))
  12.  
  13. ## order by birthrate and deathrate
  14. datasetSort = dataset %>% arrange(state, birthrate, deathrate)
  15.  
  16. # other method
  17. datasetSort_2 = dataset[order(dataset$state, dataset$birthrate,
  18.   dataset$deathrate),]
  19.  
  20. ## The hospitals with top 5 birthrate in each state
  21. datasetBirthRate5 = datasetSort %>% split(use_series(., state)) %>% lapply(head, n = 5)
  22.  
  23. # other method
  24. datasetBirthRate5_2 = lapply(split(datasetSort_2,
  25.   datasetSort_2$state), function(x) head(x, 5))
  26.  
  27. ## The hospital with the highest birthrate in each state
  28. datasetMaxBR = datasetSort %>% group_by(state) %>%
  29.   filter(birthrate==max(birthrate))
  30.  
  31. # other method
  32. datasetMaxBR_2 = do.call(ribind,
  33.   lapply(split(datasetSort_2, datasetSort_2$state),
  34.   function(x) x[which.max(x$birthrate)]))
  35.  
  36. ## The hospital with the lowest deathrate in each state
  37. datasetMinDR = datasetSort %>% group_by(state) %>%
  38.   filter(deathrate==min(deathrate))
  39.  
  40. # other method
  41. datasetMaxBR_2 = do.call(rbind,
  42.   lapply(split(datasetSort_2, datasetSort_2$state),
  43.   function(x) x[which.min(x$deathrate)]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement