deepseefan
deepseefan

Reputation: 3791

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

Upvotes: 3

Views: 537

Answers (1)

akrun
akrun

Reputation: 886938

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

Upvotes: 3

Related Questions