How to do the Random sampling of of a dataset in R having the transaction ID should be together -
my sample data set following
transactionid desc 1 1 d 1 2 c 2 d 3 l 3 g 3 h 5 h 5 b 5 h 5 f 6 d 7 f 7 v 7 f 8 f 8 d
the sampling result should
1 1 d 1 2 c 2 d 3 l 3 g 3 h
or
5 h 5 b 5 h 5 f 6 d 7 f 7 v 7 f 8 f 8 d
the exact sample values not important , can important factor have keep same transaction id should in 1 sample. how can ?
you can try
n <- 2 df[with(df, transactionid %in% sample(unique(transactionid),n, replace=false)),] # transactionid desc #1 1 #2 1 d #3 1 #17 8 f #18 8 d
data
df <- structure(list(transactionid = c(1l, 1l, 1l, 2l, 2l, 3l, 3l, 3l, 5l, 5l, 5l, 5l, 6l, 7l, 7l, 7l, 8l, 8l), desc = c("a", "d", "a", "c", "d", "l", "g", "h", "h", "b", "h", "f", "d", "f", "v", "f", "f", "d")), .names = c("transactionid", "desc"), class = "data.frame", row.names = c(na,-18l))
Comments
Post a Comment