I am trying to create a data.frame in R, but I don't have a strong background in R or in writing code. What I am trying to do is create a data.frame where each column has a range of integers without repeating. In other words, I want each column to have, for example, the number 1 through 15 in a random order. How should I go about doing this?
Asked
Active
Viewed 97 times
0
-
2Welcome to SO. You'll generally get better help if you include what you have tried so far and why it isn't working. Also, make sure to look here and on google for answers to your questions before posting.. as this has been asked before. However, to point you in a direction: take a look at the `sample` function. – Justin Jan 24 '14 at 14:51
-
2http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example the answer to this question is actually really instructive to your question. – Brandon Bertelsen Jan 24 '14 at 14:55
-
This is called a **permutation**, which might help your google search: http://people.reed.edu/~jones/141/Permutations.html – Ben Bolker Jan 24 '14 at 15:01
1 Answers
1
data.frame(a=sample(1:15,15),b=sample(1:15,15))
a b
1 12 11
2 10 12
3 3 9
4 9 3
5 7 6
6 4 8
7 1 10
8 14 7
9 15 2
10 2 4
11 6 14
12 11 5
13 8 13
14 13 15
15 5 1

Troy
- 8,581
- 29
- 32