trinker / wakefield

Generate random data sets
256 stars 28 forks source link

Generate multiple variables #6

Closed tchakravarty closed 8 years ago

tchakravarty commented 8 years ago

I am trying to create a wide dataset with multiple variables of a certain type and do not want to have to type them all out. If I were to type them all out, it would look something like this:

sample_size = 1e6
df_foo = r_data_frame(
  n = sample_size,
  ID = id,
  y = dummy,
  x1 = rnorm,
  x2  = rnorm,
  x3 = rnorm, 
  ...,
  x100 = rnorm
)

I wonder if something like this is possible with wakefield or there is a workaround to get this done?

Thanks.

trinker commented 8 years ago

Hi @tchakravarty Thanks for posting.

Can I ask did you read though the README? I would have thought r_series would have been clear to the reader? If you did read through it I need to make this clearer and your feedback here would be useful. If you did not read the README...many packages provide a README or vignette(s) that show the package use. This is a valuable first step to troubleshooting.

Using r_series this is how I'd approach:

sample_size = 1e6
df_foo = r_data_frame(
  n = sample_size,
  ID = id,
  y = dummy,
  r_series(rnorm, 100, rep.sep = "")
)

If you want the variable names exactly as you show you can make a custom function as shown below:

r_norm <- function (n, ..., name = "norm") {
    if (missing(n)) stop("`n` is missing")
    out <- rnorm(n=n, ...)
    varname(out, name)
}

df_foo = r_data_frame(
  n = sample_size,
  ID = id,
  y = dummy,
  r_series(r_norm, 100, name="x", rep.sep = "")
)
tchakravarty commented 8 years ago

Politest RTFM I ever saw. :-) I did not read through the README @trinker -- I was already a layer deep in that I needed to use wakefield to create an MWE to raise an issue about another package. But will do so. Thanks.

trinker commented 8 years ago

thanks for the honesty :-) I'm going to close if it doesn't work let me know (reopen this).