Thursday 15 April 2010

r - How to shuffle columns independently -



r - How to shuffle columns independently -

how shuffle/permute/randomize r values independently in columns in big info frame , remain order of columns. example:

> m head1 head2 head3 1 1 2 2 b b 3 3 c c 4 4 d d > m head1 head2 head3 1 2 b d 2 4 3 1 d b 4 3 c c

the straightforward way utilize lapply , sample, this:

m[] <- lapply(m, sample)

or, "data.table":

as.data.table(m)[, lapply(.sd, sample)]

demo:

> m head1 head2 head3 1 1 2 2 b b 3 3 c c 4 4 d d > set.seed(1) ## can reproduce results > m[] <- lapply(m, sample) > m head1 head2 head3 1 2 c 2 4 c 3 3 b d 4 1 d b

r

No comments:

Post a Comment