Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (df <- data.frame(x = sample(1:10, 20, replace = TRUE)))
- #> x
- #> 1 7
- #> 2 10
- #> 3 4
- #> 4 9
- #> 5 10
- #> 6 4
- #> 7 7
- #> 8 4
- #> 9 2
- #> 10 2
- #> 11 1
- #> 12 9
- #> 13 4
- #> 14 3
- #> 15 2
- #> 16 6
- #> 17 5
- #> 18 7
- #> 19 4
- #> 20 9
- (unique_df <- unique(df))
- #> x
- #> 1 7
- #> 2 10
- #> 3 4
- #> 4 9
- #> 9 2
- #> 11 1
- #> 14 3
- #> 16 6
- #> 17 5
- unique_df$idx <- rownames(unique_df)
- unique_df
- #> x idx
- #> 1 7 1
- #> 2 10 2
- #> 3 4 3
- #> 4 9 4
- #> 9 2 9
- #> 11 1 11
- #> 14 3 14
- #> 16 6 16
- #> 17 5 17
- ## 簡化語法之後
- unique_df <- cbind(unique(df), idx = rownames(unique(df)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement