I have some data that involves zebu (beef animals) that are labeled 1-40. I need to divide them into 4 groups of 10 each. I need to choose them randomly to remove any bias and I need to use R and Excel. Thank you please help.
Asked
Active
Viewed 81 times
-2
-
3If you can't share any data with us or any efforts you have tried, then you are not ready to ask a question yet. – Tim Biegeleisen Aug 03 '17 at 11:41
-
try look at `?sample` – simone Aug 03 '17 at 11:44
-
https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Mal_a Aug 03 '17 at 11:57
2 Answers
2
There are ways of doing this that only require less code, but here's a verbose example that let's me explain what's happening.
Here's the dataset I'll be using since I don't know exactly how your data look.
beef <-
data.frame(number = 1:40, weight = round(rnorm(40, mean = 2000, sd = 500)))
Because your animals are numbered from 1 to 40, you can create a new dataframe that contains those numbers with a random group number (1 to 4) as the second column.
num_group <- (data.frame(
number = 1:40,
group =
sample(
x = 1:4,
size = 40,
replace = TRUE
)
))
Join the two dataframes together and you have your answer.
merge(beef, num_group)

Andrew Brēza
- 7,705
- 3
- 34
- 40
-
If you want to do this using only Excel, you could add a new column with `=RANDBETWEEN(1,4)`. – Andrew Brēza Aug 03 '17 at 14:43
1
To shuffle the data in excel follow this tip
Create new column in your data then apply RAND() It will generate random number over that column and sort random numbers column you will get your data shuffled.
Later load data in to R and select 10 rows each time and assign class to them.

RAVI TEJA M
- 151
- 4