[R] Exporting a list of lists

rkevinburton at charter.net rkevinburton at charter.net
Tue Aug 12 00:57:46 CEST 2008


I tried:

cat("Sku,Shape,Scale,DayOfYear", file="SkuSalesInfo.dat", append=FALSE); 
for(i in 1:length(mlist))
{
    cat(mlist[[i]]$Sku, ",", mlist[[i]]$Shape, ",", mlist[[i]]$Scale, file="SkuSalesInfo.dat", append=TRUE)
    for(j in 1:length(mlist[[i]]$DayOfYear))
    {
      cat(",", mlist[[i]]$DayOfYear[j], file="SkuSalesInfo.dat", append=TRUE)
    }
    cat("\n", file="SkuSalesInfo.dat", append=TRUE)
}

But it is EXTREMELY slow.

Kevin

---- rkevinburton at charter.net wrote: 
> I would like to read it (the data) iinto a different application (not 'R'). Within 'R' I can
> 
> mlist[[1]]
> > mlist[1]
> [[1]]
> [[1]]$Sku
> [1] "0"
> 
> [[1]]$Shape
> [1] 250.0586
> 
> [[1]]$Scale
> [1] 91.9914
> 
> [[1]]$DayOfYear
> [1] 250
> 
> OR
> 
> mlist[[2]]
> > mlist[[2]]
> $Sku
> [1] "100008"
> 
> $Shape
> [1] 178.9637
> 
> $Scale
> [1] 58.76785
> 
> $DayOfYear
>  [1]  82  89  93 108 123 123 125 185 186 260 261 266 266 270 270 270 270 273 276 276 276 277 277 277 277 278 278 279 341 357
> 
> > 
> I essentially want to mimic this structure with the write.table but that is not what I am seeing. If I take the variable length list off then I get what appears to be a autogenerated list of headers for every row in the file. It is like the fact that this is a list of lists is forgotten. 
> 
> For each line of the file being something like (for the first two list elements above)
> 
> 0,250.0586,91.9914,250
> 100008,178.9637,58.767,85,82 ,89 ,93,108,123,123,125,185,186,260,261,266,266,270,270,270,270,273,276,276,276,277,277,277,277,278,278,279,341,357
> 
> Where 100008 is the part number (Sku), 178.9637 is the Shape, 58.767 is the Scale, and the remaining is the day of year list (variable length). There would be one of these rows for every part number. If I use dput I get more than I want and it doesn't mean anything to the external application that I am exporting the data to. Similarily 'dump' gives me a bunch of 'R' specific information about the data structure that an external application will not necessarily know what to do with. I just want a simple one row output as able.
> 
> If I just write.table(mlist,.....) then I get the aforementioned warning about different lengths. If I iterate though the list:
> 
> for(i in 1:length(mlist))
> {
>     write.table(mlist[[i]].....)
> }
> 
> Then I get a "50 or more warnings" error.
>     50: In write.table(mlist[[i]], "SkuSalesInfo.dat", row.names = FALSE,  ... :
>       appending column names to file
> 
> The file that is generated is HUGE (> 250Mb). It seems to be duplicating the row the by the number of elements in the variable list. So instead of one row I get:
> "Sku","Shape","Scale","DayOfYear"
> "100008",178.963659119674,58.7678453522722,82
> "100008",178.963659119674,58.7678453522722,89
> "100008",178.963659119674,58.7678453522722,93
> "100008",178.963659119674,58.7678453522722,108
> "100008",178.963659119674,58.7678453522722,123
> "100008",178.963659119674,58.7678453522722,123
> "100008",178.963659119674,58.7678453522722,125
> "100008",178.963659119674,58.7678453522722,185
> "100008",178.963659119674,58.7678453522722,186
> "100008",178.963659119674,58.7678453522722,260
> "100008",178.963659119674,58.7678453522722,261
> "100008",178.963659119674,58.7678453522722,266
> "100008",178.963659119674,58.7678453522722,266
> "100008",178.963659119674,58.7678453522722,270
> "100008",178.963659119674,58.7678453522722,270
> "100008",178.963659119674,58.7678453522722,270
> "100008",178.963659119674,58.7678453522722,270
> "100008",178.963659119674,58.7678453522722,273
> "100008",178.963659119674,58.7678453522722,276
> "100008",178.963659119674,58.7678453522722,276
> "100008",178.963659119674,58.7678453522722,276
> "100008",178.963659119674,58.7678453522722,277
> "100008",178.963659119674,58.7678453522722,277
> "100008",178.963659119674,58.7678453522722,277
> "100008",178.963659119674,58.7678453522722,277
> "100008",178.963659119674,58.7678453522722,278
> "100008",178.963659119674,58.7678453522722,278
> "100008",178.963659119674,58.7678453522722,279
> "100008",178.963659119674,58.7678453522722,341
> "100008",178.963659119674,58.7678453522722,357
> 
> The header seems to be duplicated for each element also.
> 
> Any ideas?
> 
> Thank you.
> 
> Kevin
> 
> ---- jim holtman <jholtman at gmail.com> wrote: 
> > What do you want to do with the data?  Are you just storing it to read
> > in it later?  Have you looked at 'save/load'?  If you want a character
> > representation, try 'dput'; this can be read back in with 'source'.
> > So it all depends on what you are planning to do with it.
> > 
> > On Mon, Aug 11, 2008 at 4:14 PM,  <rkevinburton at charter.net> wrote:
> > > I have a list
> > >
> > > List(Sku=" ", Shape=1, Scale=3, DayOfYear=daylist)
> > > Note: picture daylist as c(2,3,4,3) it is a list with variable length.
> > >
> > > Then I have a list of lists
> > >
> > > al <- c(al, List(List(Sku=" ", Shape=1, Scale=3, DayOfYear=daylist))
> > > Note: same comment on daylist as above.
> > >
> > > So far this creates a list of lists just how I want it. If I do al[1] I get each member and the variable length list 'daylist'.
> > >
> > > Now I want to export this list of lists:
> > >
> > > write.table(mlist, "SkuInfo.dat", row.names = FALSE,  sep=",")
> > >
> > > But I get an error that the list lengths are not equal. The only member of this list that has a variable length would be DayOfYear
> > >
> > > How can I output this list of lists when each element list contains a variable length list?
> > >
> > > Thank you.
> > >
> > > Kevin
> > >
> > > ______________________________________________
> > > R-help at r-project.org mailing list
> > > https://stat.ethz.ch/mailman/listinfo/r-help
> > > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> > > and provide commented, minimal, self-contained, reproducible code.
> > >
> > 
> > 
> > 
> > -- 
> > Jim Holtman
> > Cincinnati, OH
> > +1 513 646 9390
> > 
> > What is the problem that you are trying to solve?
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list