Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

making a loop to create dummy data

I am trying to create a loop for making a series of dummy data files. Basically, I want a loop to create three dataframes: h00_2000, h10_2000, s00_2000, with the associated x values of h00_, h10_, and s00 for naming purposes. I want them to have three columns each with 5 observations, x_city, x_race, and id. x_city should have a random string, x_race should be 1 or 2, and id should be random numbers. This is where I am at, but the loop doesn’t work, and I don’t know how to assign it to the dataframe names I’ve given:

for(x in c("h00_", "h10_", "s00_")){
paste0(x, "city") <- do.call(paste0, replicate(5, sample(LETTERS, 50, TRUE), FALSE))
paste0(x, "race") <- do.call(paste0, replicate(1, sample(1:2, 50, TRUE), FALSE))
id <-  do.call(paste0, replicate(5, sample(0:9, 50, TRUE), FALSE))
}

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You can use assign:

for(x in c("h00_", "h10_", "s00_")){
  assign(paste0(x, "2000"), data.frame(
    city = do.call(paste0, replicate(5, sample(LETTERS, 50, TRUE), FALSE)),
    race =  do.call(paste0, replicate(1, sample(1:2, 50, TRUE), FALSE)),
    id =  do.call(paste0, replicate(5, sample(0:9, 50, TRUE), FALSE))))
}

head(h00_2000)
#>    city race    id
#> 1 LCJUC    2 51629
#> 2 TEQLS    2 20029
#> 3 OWNQB    2 51191
#> 4 LZXHI    2 02100
#> 5 CYOPQ    1 26055
#> 6 JCXHT    2 50408
head(h10_2000)
#>    city race    id
#> 1 EJFHL    2 33186
#> 2 VBPID    1 10350
#> 3 PSQLN    2 21706
#> 4 GYMPY    1 68808
#> 5 CMKUS    2 64416
#> 6 ERELS    1 26259
head(s00_2000)
#>    city race    id
#> 1 WOWMI    2 95953
#> 2 JAGIW    1 32360
#> 3 NMVYS    2 83824
#> 4 TDQZO    1 44549
#> 5 QTIWO    1 39824
#> 6 WJPUU    2 29531
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading