0

I want to create a number of data frames which take their name from a list.

eg.

mylist <- c("home", "work", "pub")
for (i in mylist) {
    name <- i
    Hourly  <<- ddply(get(i), .(week = week(StartTime), Place = name)
}

This produces 1 dataframe called Hourly with week and name variables.

What I want is 3 dataframes called home_hourly, work_hourly, pub_hourly each containing their respective 2 variables. How do I produce 3 dataframes, prefixing each with name?

plannapus
  • 18,529
  • 4
  • 72
  • 94
felixmc
  • 516
  • 1
  • 4
  • 19

1 Answers1

0

I think what you need is a combination of the assign() function and the paste() function.

mylist <- c("home", "work", "pub")
for (i in mylist) {
    name <- i
    assign(paste(name, "hourly", sep = "-"), ddply(get(i), .(week = week(StartTime), Place = name))
}
jinlong
  • 839
  • 1
  • 9
  • 19
  • Using `sep="-"` will create problematic names, that can only be referenced with backticks. – Ferdinand.kraft Sep 17 '13 at 20:18
  • Thank you for your responses - they are all appreciated - but perhaps some were a bit quick to mark as duplicate? 'How to name variables on the fly in R?" does indeed answer the question of creating a named variable but not the dynamic population of each variable. For that you do need paste and assign: `for (i in mylist) {` `onThisI <- paste(i,"Unit",sep="_")` `assign(onThisI, ddply(get(i), .(week = as.factor(week(StartTime)), Unit = i)) }` – felixmc Sep 18 '13 at 14:57