Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Randomize dataframe in r

A dataframe which is randomized and sorted using Object column is shown below as an example. The order is applied to sample only to organize similar objects together. Will it be possible to avoid the order operation and achieve randomization within the repeating Object column? Thanks.

set.seed(7)
# read csv
df <- read.csv("~/Documents/test.csv", header = T)
df
     Object Tag Comment
1         1   A      C1
2         1   B      C2
3         1   C      C3
4         1   D      C4
5         2   A      A1
6         2   C      A2
7         2   F      A3
8         2   G      A4
9         3   P      C1
10        3   N      A1
11        3   P      B5
# randomize the order of the df
dfr <- df[sample(nrow(df)),]
dfr
    Object Tag Comment
11      3   P      B5
4       1   D      C4
2       1   B      C2
1       1   A      C1
9       3   P      C1
5       2   A      A1
7       2   F      A3
10      3   N      A1
8       2   G      A4
3       1   C      C3
6       2   C      A2 
#sort dfr using Object field
dfrSort <- dfr[with(dfr, order(Object)), ]
dfrSort
     Object Tag Comment
4         1   D      C4
2         1   B      C2
1         1   A      C1
3         1   C      C3
5         2   A      A1
7         2   F      A3
8         2   G      A4
6         2   C      A2
11        3   P      B5
9         3   P      C1
10        3   N      A1
like image 879
deepseefan Avatar asked Apr 08 '26 12:04

deepseefan


1 Answers

After grouping by 'Object' do the sampleing to not break the ties

library(dplyr)
df %>%
     group_by(Object) %>% 
     slice(sample(row_number()))
# A tibble: 11 x 3
# Groups:   Object [3]
#   Object   Tag Comment
#    <int> <chr>   <chr>
# 1      1     C      C3
# 2      1     B      C2
# 3      1     A      C1
# 4      1     D      C4
# 5      2     C      A2
# 6      2     F      A3
# 7      2     G      A4
# 8      2     A      A1
# 9      3     P      B5
#10      3     P      C1
#11      3     N      A1
like image 60
akrun Avatar answered Apr 10 '26 03:04

akrun



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!