hnguyen
hnguyen

Reputation: 814

Combine two sequences into a data frame in R returned missing identifications

I want to make a data frame of 432 rows with

place_holder <- data.frame(Cohort = rep(seq(1:6),8),
                           Quadrat = rep(seq(1:8),6))

such that each cohort is present in all quadrats. What I got is

table(place_holder$Cohort, place_holder$Quadrat)
   
     1  2  3  4  5  6  7  8
  1 18  0 18  0 18  0 18  0
  2  0 18  0 18  0 18  0 18
  3 18  0 18  0 18  0 18  0
  4  0 18  0 18  0 18  0 18
  5 18  0 18  0 18  0 18  0
  6  0 18  0 18  0 18  0 18

What I expect is all 9's when I do the table() command. How do I change my code. Thanks a lot!!

Upvotes: 1

Views: 90

Answers (2)

U13-Forward
U13-Forward

Reputation: 71600

One of your columns needs to be ordered, you need to specify the each argument instead:

place_holder <- data.frame(Cohort = rep(seq(1:6), 72),
                           Quadrat = rep(seq(1:8), each = 54))

And then:

table(place_holder$Cohort, place_holder$Quadrat)

Or if you already defined the data frame and you can't change it, try:

table(place_holder$Cohort, sort(place_holder$Quadrat))
   

All output:

    1 2 3 4 5 6 7 8
  1 9 9 9 9 9 9 9 9
  2 9 9 9 9 9 9 9 9
  3 9 9 9 9 9 9 9 9
  4 9 9 9 9 9 9 9 9
  5 9 9 9 9 9 9 9 9
  6 9 9 9 9 9 9 9 9

Upvotes: 1

Kra.P
Kra.P

Reputation: 15143

You can add each argument to one of variables.

place_holder <- data.frame(Cohort = rep(seq(1:6),each = 72),
                           Quadrat = rep(seq(1:8),54))

table(place_holder$Cohort, place_holder$Quadrat)

    1 2 3 4 5 6 7 8
  1 9 9 9 9 9 9 9 9
  2 9 9 9 9 9 9 9 9
  3 9 9 9 9 9 9 9 9
  4 9 9 9 9 9 9 9 9
  5 9 9 9 9 9 9 9 9
  6 9 9 9 9 9 9 9 9

Upvotes: 2

Related Questions