Advertisement
Guest User

Untitled

a guest
Jun 29th, 2016
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.22 KB | None | 0 0
  1. # Let us suppose I am using this built-in dataset to draw up a
  2. # shortlist of where I might wish to go on holiday
  3. df <- data.frame(state.x77);
  4.  
  5. # pp - define a task-specific pretty print function
  6. pp <- function(row) {
  7. print(row); # Example dataset is simple enough to just print the entire row
  8. }
  9.  
  10. # cls - clear the screen (this hack works on Windows but I've commented it for now)
  11. cls <- function() {
  12. #system("powershell -ExecutionPolicy Bypass -command (New-Object -ComObject Wscript.Shell).SendKeys([string][char]12)");
  13. }
  14.  
  15. # It would halve the number of keystrokes needed if I knew a way to read
  16. # a single character
  17. readcharacter <- readline;
  18.  
  19. sift <- function(df, pp)
  20. {
  21. classification = rep('', nrow(df));
  22.  
  23. for (nRow in 1:nrow(df))
  24. {
  25. cls();
  26. pp(df[nRow,]);
  27. cat("nEnter 'a' to discard, 'd' to keep, 'q' to quitn");
  28.  
  29. char <- '';
  30. while (char != 'a' && char != 'd' && char != 'q') {
  31. char <- readcharacter();
  32. }
  33.  
  34. if (char == 'q')
  35. break;
  36.  
  37. classification[nRow] = char;
  38. }
  39.  
  40. return(cbind(df,classification=classification));
  41. }
  42.  
  43. result = sift(df, pp);
  44.  
  45. cls();
  46. cat("Shortlist:n");
  47. print(row.names(result[result$classification=='d',]));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement