Advertisement
hohiyan

ptt-r-20220920

Jul 19th, 2022 (edited)
470
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 0.66 KB | None | 0 0
  1. (df <- data.frame(x = sample(1:10, 20, replace = TRUE)))
  2. #>     x
  3. #> 1   7
  4. #> 2  10
  5. #> 3   4
  6. #> 4   9
  7. #> 5  10
  8. #> 6   4
  9. #> 7   7
  10. #> 8   4
  11. #> 9   2
  12. #> 10  2
  13. #> 11  1
  14. #> 12  9
  15. #> 13  4
  16. #> 14  3
  17. #> 15  2
  18. #> 16  6
  19. #> 17  5
  20. #> 18  7
  21. #> 19  4
  22. #> 20  9
  23.  
  24. (unique_df <- unique(df))
  25. #>     x
  26. #> 1   7
  27. #> 2  10
  28. #> 3   4
  29. #> 4   9
  30. #> 9   2
  31. #> 11  1
  32. #> 14  3
  33. #> 16  6
  34. #> 17  5
  35.  
  36. unique_df$idx <- rownames(unique_df)
  37.  
  38. unique_df
  39. #>     x idx
  40. #> 1   7   1
  41. #> 2  10   2
  42. #> 3   4   3
  43. #> 4   9   4
  44. #> 9   2   9
  45. #> 11  1  11
  46. #> 14  3  14
  47. #> 16  6  16
  48. #> 17  5  17
  49.  
  50.  
  51. ## 簡化語法之後
  52. unique_df <- cbind(unique(df), idx = rownames(unique(df)))
  53.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement