Here, each city has a different number of citizens. And the value of
N used to create the age variable automatically updates as needed. The result is a dataset with 6 citizens, 2 in the first city and 4 in the second. As long as N is either a number, or a vector of the same length of the current lowest level of the data,
level() will know what to do.
It is also possible to provide a function to N, enabling a random number of citizens per city:
Here, each city is given a random number of citizens between 1 and 6. Since the
sample() function returns a vector of length 2, this is like specifying 2 separate
Ns as in the example above.
Finally, it is possible to define
N on the basis of higher level variables themselves. Consider the following example:
Here, the city has a defined population, and the number of citizens in our simulated data reflects a sample of 30% of that population. Each city has a different population, so each city gets a different number of citizens in this example.
You may want to include the mean value of a variable within a group defined by a higher level of the hierarchy, for example the average income of citizens within city. You can do this with
Because the functions in fabricatr take data and return data, they are easily slotted into a
my_data <- data_frame(Y = sample(1:10, 2)) %>% fabricate(lower_level = level(N = 3, Y2 = Y + rnorm(N))) my_data