Guest User

Untitled

a guest
Apr 19th, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.78 KB | None | 0 0
  1. library(dplyr)
  2.  
  3. # create a function
  4. my_summaries <- function(x){
  5. # error handling goes here
  6. my_mutate <- x %>% mutate(kmh = dist/speed)
  7. my_summary <- my_mutate %>% summary()
  8. list(mutate = my_mutate, summary = my_summary)
  9. }
  10.  
  11. my_data <- my_summaries(cars)
  12.  
  13. str(my_data)
  14. List of 2
  15. $ mutate :'data.frame': 50 obs. of 3 variables:
  16. ..$ speed: num [1:50] 4 4 7 7 8 9 10 10 10 11 ...
  17. ..$ dist : num [1:50] 2 10 4 22 16 10 18 26 34 17 ...
  18. ..$ kmh : num [1:50] 0.5 2.5 0.571 3.143 2 ...
  19. $ summary: 'table' chr [1:6, 1:3] "Min. : 4.0 " "1st Qu.:12.0 " "Median :15.0 " "Mean :15.4 " ...
  20. ..- attr(*, "dimnames")=List of 2
  21. .. ..$ : chr [1:6] "" "" "" "" ...
  22. .. ..$ : chr [1:3] " speed" " dist" " kmh"
  23.  
  24.  
  25. # Unlist list of data.frames
  26. list2env(my_data ,.GlobalEnv)
Add Comment
Please, Sign In to add comment