HermanK
HermanK

Reputation: 309

How to randomly select 2 cells from a horizontal range in Google Sheet or R

I need to pick randomly 2 cells from a row (specific range/columns of the row).

Al  Em  Kev
1   1   2
3   2   3
2   2   1
3   3   3

output:

rand 1  rand 2
2         1
3         2
2         2
3         3

The results need to be unique: the same cell cannot be returned both times.

It would be quicker to do it in Google sheets right now, but an answer using R would be fine as well.

Thanks guys

Upvotes: 1

Views: 282

Answers (2)

player0
player0

Reputation: 1

=SPLIT(INDIRECT(CHAR(RANDBETWEEN(65,67))&ROW())&
   "♥"&INDIRECT(CHAR(RANDBETWEEN(65,67))&ROW()),"♥")

0

Upvotes: 0

akrun
akrun

Reputation: 887158

We can do this with apply

t(apply(data, 1, function(x) sample(x, 2))) 

Upvotes: 1

Related Questions