0

I am trying to create a list of data frames within within a loop by subsetting main data within a loop. However it throws out this error

Error in `[.data.frame`(top_mfr, i) : undefined columns selected
In addition: Warning messages:
1: In fata1[i] <- subset(data, data$PROD_MANUFACTURER == top_mfr[i]) :
  number of items to replace is not a multiple of replacement length
2: In fata1[i] <- subset(data, data$PROD_MANUFACTURER == top_mfr[i]) :
  number of items to replace is not a multiple of replacement length

The code fragment is as follows.

for(i in 1:length(top_mfr))
{ data1[i]<-subset(data,data$PROD_MANUFACTURER==top_mfr[i])}
m0nhawk
  • 20,919
  • 9
  • 39
  • 68
  • when you put `data1[i]` you didn't specify a column. Do `data1[i,1]` for the first column and so on. Also it is best to add a reproducible example : http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example (using `dput(yourdata)`helps a lot) – etienne Nov 02 '15 at 16:41
  • Or, if `data1` is a list then use `[[` to assign to a single element, `data1[[i]] – Gregor Thomas Nov 02 '15 at 16:42
  • Do not use the dataset name in the 'subset' argument to `subset`. Learn to use %in% instead of "==". (I don't know if these are all of your errors because it appears the error was actually thrown from a misspelling of your "data1" object name as "fata1". – IRTFM Nov 02 '15 at 16:43
  • Though, if that's the case, then rather than the `for` loop you could do `d = subset(data, PROD_MANUFACTURER %in% top_mfr); data1 = split(d, f = d$PROD_MANUFACTURER)` – Gregor Thomas Nov 02 '15 at 16:44
  • @Gregor Spot On! Thanks – Unni Sivakumar Nov 02 '15 at 16:50
  • I think this is best covered in the marked dupe (specifically about lists of data frames - my answer there covers the `split()` method), but if you want more reading on `[` vs `[[`, [see here](http://stackoverflow.com/q/1169456/903061). – Gregor Thomas Nov 02 '15 at 17:34

0 Answers0