-2

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.

Andrew Brēza
  • 7,705
  • 3
  • 34
  • 40
henry
  • 9

2 Answers2

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
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