5

I would like to create multiple data frames and assign them based on years. I have seen other posts but I couldn't duplicate it for my case. For example,

a <- c(1,2,3,4)
b <- c('kk','km','ll','k3')
time <- (2001,2001,2002,2003)
df <- data.frame(a,b,time)
myvalues <- c(2001,2002,2003)
for (i in 1:3) 
{ y[[i]]<- df[df$time=myvalues[[i]],}

I would like to create three dataframes y1, y2, y3 for years 2001, 2002 and 2003. Any suggestions how do using a for loop?

Parfait
  • 104,375
  • 17
  • 94
  • 125
user3570187
  • 1,743
  • 3
  • 17
  • 34
  • 3
    You have three problems - you need to initialize the list before the loop, `y = list()`, use `==` not `=` for testing equality, and you have unmatched brackets in your loop, `{y[[i]] <- df[df$time == myvalues[i],]}`. However instead of the for loop you can just do `y = split(df, df$time)` – Gregor Thomas Jun 15 '17 at 19:11
  • 1
    See also [how to make a list of data frames](https://stackoverflow.com/a/24376207/903061) (possible dupe?). – Gregor Thomas Jun 15 '17 at 19:12
  • @Gregor I would say so – Sotos Jun 15 '17 at 19:29
  • He says he wants 3 dataframes. Not a list of 3 dataframes. – psychOle Jun 15 '17 at 19:38
  • yes exactly, i was able to get the list from the previous posts and I am looking for individual data frames – user3570187 Jun 15 '17 at 19:39
  • 2
    ok, so `list2env(split(df, df$time), .GlobalEnv)` – Sotos Jun 15 '17 at 19:40
  • (But in all likelihood you *should* want a list of data frames, not individual data frames. There are very few cases where a list is not better and easier to work with than sequentially named variables). – Gregor Thomas Jun 15 '17 at 19:58
  • I need individual ones as I have to carryout some operations on them before combining to a large dataset – user3570187 Jun 15 '17 at 20:25

1 Answers1

9

The assign() function is made for this. See ?assign() for syntax.

a <- c(1,2,3,4)
b <- c("kk","km","ll","k3")
time <- c(2001,2001,2002,2003)
df <- data.frame(a,b,time)
myvalues <- c(2001,2002,2003)

for (i in 1:3) {
  assign(paste0("y",i), df[df$time==myvalues[i],])
  }

See here for more ways to achieve this.

psychOle
  • 1,054
  • 9
  • 19