0

I want to create 26 data frames from a raw data (dat) I have based on certain condition. dat1 is the data where zipid1 = 1, dat2 is the data where zipid2 = 1, ect..

My current data looks like this:

#### create dummy data  ------------------------------------------------------------------------
dat <- data.frame(
  outc1 = rnorm(10, mean = 1, sd = 0.5),
  outc2 = runif(10, min = 0.2, max = 1),
  zipid1 = sample(c(1, 0), 10, replace = T),
  zipid2 = sample(c(1, 0), 10, replace = T),
  zipid3 = sample(c(1, 0), 10, replace = T),
  zipid4 = sample(c(1, 0), 10, replace = T),
  zipid5 = sample(c(1, 0), 10, replace = T)
)

And my want data looks liks this:

#### want data looks like this -----------------------------------------------------------------
dat1 <- dat %>%
  filter(zipid1 == 1)

dat2 <- dat %>%
  filter(zipid2 == 1)

dat3 <- dat %>%
  filter(zipid3 == 1)

dat4 <- dat %>%
  filter(zipid4 == 1)

dat5 <- dat %>%
  filter(zipid5 == 1)

In reality I have zipid1-zipid26, how can I write a concise code to create 26 datasets at once?

Thanks!!

mandy
  • 483
  • 9
  • 20
  • You're filtering based on `filter(zipidi == 1)` not `filter(zipidi == i)`, which is probably what you want. Also, is the variable `zipid` or `zipidi`? – divibisan May 21 '18 at 21:34
  • Your data structure is not very clear. It is better if you provide a reproducible example. My guess is you are looking for `myList <- lapply(1:10, function(i) dat[dat[[paste0("zipid", i)]] == 1,])` where you have variables zipdi1 through zipid10. – lmo May 21 '18 at 23:32
  • Thanks @lmo, I think the myList you created includes all the sub data I want, but how can I transform each of them as individual data? Without typing this for 10 times? `dat1 <- as.data.frame(myList[1])` – mandy May 22 '18 at 14:36

1 Answers1

0

I found a solution here based on @lmo's answer.

First, use lapply function to create a list:

myList <- lapply(1:10, function(i) dat[dat[[paste0("zipid", i)]] == 1,])

Then write a loop to extract each data set: (this is based on answer from another question: Extracting data.frames from a list using for loop)

for (i in 1:10) {
  assign(paste0("dat", i), as.data.frame(myList[i]))
}
mandy
  • 483
  • 9
  • 20
  • It is typically preferable to keep similar data.frames in a list as this can be quite useful for repeated operations. See gregor's answer to [this post](https://stackoverflow.com/questions/17499013/how-do-i-make-a-list-of-data-frames) for some examples. If you really want to extract the data.frames, use `list2env`. This works for named lists. In general it is preferable to avoid using `assign`. – lmo May 26 '18 at 10:53