0

I am doing analysis where a set of cross tabs have to be generated for a large set of distributors. The data is at a customer level and each customer is mapped to a distributor. Since the number of distributors are large, I am trying to automate the process.

I am getting stuck at dynamically creating data frames based on the distributor ID

Here is what I am trying:

for (i in 1:length(DiD)){ #vector comprising list of distributors
    paste("use",DiD[[1]],sep="_") <- subset(master table, Field1=="NA"& Field2=="valid" & Field3==as.character(DiD[[1]])) 
}

Additional Info:

DiD[[1]] = 1234

Desired output: A data frame use_1234 which contains the master data subset by Field1, Field2 and by DiD=1234

Errors Thrown: Facing issues with the paste part as well with specifying Field3 as DiD[[1]]

Error in eval(expr, envir, enclos) :
dims [product 1] do not match the length of object [529]

I hope that I've provided sufficient information. Thanks a ton!

PS: Apologize if the questions has been answered in the forum, I could not find it.

Thomas
  • 43,637
  • 12
  • 109
  • 140
Raama Vi
  • 15
  • 1
  • 7
  • You may want to look at `assign`. Also, it's good to avoid `subset` and instead use `[`. – Thomas Jul 04 '13 at 15:06
  • Thanks @Thomas , I will check assign. The other issue is to get the data frame name dynamically created. Any views on what might be going wrong there? – Raama Vi Jul 04 '13 at 15:09
  • @agstudy has given you a good answer using `assign` that will probably solve your issue. – Thomas Jul 04 '13 at 15:10
  • I will try @agstudy, I'll add some dummy tables to make the question more clear.Thanks for the suggestion. I will also add an answer once I have cracked this – Raama Vi Jul 04 '13 at 15:28
  • @RaamaVi You should read [this to-make-a-great-r-reproducible-example.](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – agstudy Jul 04 '13 at 15:31

2 Answers2

1

No need to use for here, subset will generate a data.frame:

 res <- subset(master.table,Field1== NA & 
                            Field2=="valid" & 
                            Field3==as.character(DiD[[1]])) 

then you can use assign :

  assign(paste("use",DiD[[1]],sep="_"),res)

I guess , you need something like this , assuming that DiD is a list of names:

 list.df <- lapply(DiD, function(x)  subset(master.table,is.na(Field1) & 
                            Field2=="valid" & 
                            Field3==as.character(x)))
 names(list.df) <- DiD

This will create a named list of data.frames.

agstudy
  • 119,832
  • 17
  • 199
  • 261
  • Thanks @agstudy . I'll try including the 2 code fragments within a for loop to take care of all data frames DiD[1:n] – Raama Vi Jul 04 '13 at 15:14
  • @RaamaVi no need , used `lapply` is a loop, So DiD is a data.frame or a list? can you add `str(DiD)` – agstudy Jul 04 '13 at 15:17
0

I think you could also solve this using split

dfs <- 
split(master.table[master.table$Field1=="NA" & # probably should be is.na(master.table$Field1), but we don't have the data
                   master.table$Field2=="valid",], Field3)
Thomas
  • 43,637
  • 12
  • 109
  • 140