How to use names(mylist) within l_ply

310 views
Skip to first unread message

Mark Na

unread,
Dec 8, 2009, 8:40:34 AM12/8/09
to manip...@googlegroups.com
Hi,

I have the following code, to write multiple dataframes to separate Excel worksheets:

#make a list of the above dataframes, for llply processing (see below for str(datalist) output)
datalist<-list(data1,data2,data3,data4,data5,data6)
names(datalist)<-paste("data", 1:6, sep="")

#then I have some code that uses llply to apply functions to each of the list elements (deleted here for brevity)

#now, write each list element to an Excel worksheet (adapted from the Learning R blog at http://bit.ly/103oaP)
save2excel <- function(x) sqlSave(xlsFile,
     x, tablename = names(datalist), rownames = FALSE)
xlsFile<-odbcConnectExcel("results.xls",readOnly=FALSE)
l_ply(datalist,save2excel)
odbcCloseAll()


I need to adapt the argument that names the Excel worksheets (which is now "tablename = names(datalist)") so it names each worksheet according the name of the list element.

I'd appreciate any help, thanks!

Mark


Here's the structure of my table, in case that helps. Each table element is called "data1", "data2" ... "data6"


> str(datalist)
List of 6
 $ data1:'data.frame':  15 obs. of  10 variables:
  ..$ Pond           : Factor w/ 112 levels "1","10","100",..: 9 18 20 33 36 41 46 56 58 71 ...
  ..$ June_2007_Depth: num [1:15] 150 135 139 103 280 ...
  ..$ June_2007_Area : num [1:15] 5404 3643 6531 5479 1512 ...
  ..$ May_2009_Depth : num [1:15] 92 60 85 37 190 ...
  ..$ May_2009_Area  : num [1:15] 3164 1827 2264 2580 757 ...
 $ data2:'data.frame':  16 obs. of  10 variables:
  ..$ Pond           : Factor w/ 112 levels "1","10","100",..: 9 18 20 33 36 41 46 56 58 71 ...
  ..$ June_2007_Depth: num [1:16] 150 135 139 103 280 ...
  ..$ June_2007_Area : num [1:16] 5404 3643 6531 5479 1512 ...
  ..$ May_2008_Depth : num [1:16] 122 78 122 74 229 ...
  ..$ May_2008_Area  : num [1:16] 3708 2368 4117 4296 813 ...
 $ data3:'data.frame':  16 obs. of  10 variables:
  ..$ Pond           : Factor w/ 112 levels "1","10","100",..: 9 18 20 33 36 41 46 56 58 71 ...
  ..$ June_2007_Depth: num [1:16] 150 135 139 103 280 ...
  ..$ June_2007_Area : num [1:16] 5404 3643 6531 5479 1512 ...
  ..$ July_2008_Depth: num [1:16] 103 57 108 54 209 ...
  ..$ July_2008_Area : num [1:16] 2774 1436 3630 2075 585 ...
 $ data4:'data.frame':  17 obs. of  10 variables:
  ..$ Pond           : Factor w/ 112 levels "1","10","100",..: 1 9 18 20 33 36 41 46 56 58 ...
  ..$ May_2008_Depth : num [1:17] 338 122 78 122 74 ...
  ..$ May_2008_Area  : num [1:17] 87087 3708 2368 4117 4296 ...
  ..$ July_2008_Depth: num [1:17] 327 103 57 108 54 209 111 224 62 87.5 ...
  ..$ July_2008_Area : num [1:17] 86935 2774 1436 3630 2075 ...
 $ data5:'data.frame':  16 obs. of  10 variables:
  ..$ Pond          : Factor w/ 112 levels "1","10","100",..: 1 9 18 20 33 36 41 46 56 58 ...
  ..$ May_2008_Depth: num [1:16] 338 122 78 122 74 ...
  ..$ May_2008_Area : num [1:16] 87087 3708 2368 4117 4296 ...
  ..$ May_2009_Depth: num [1:16] 316 92 60 85 37 ...
  ..$ May_2009_Area : num [1:16] 75548 3164 1827 2264 2580 ...
 $ data6:'data.frame':  16 obs. of  10 variables:
  ..$ Pond           : Factor w/ 112 levels "1","10","100",..: 1 9 18 20 33 36 41 46 56 58 ...
  ..$ July_2008_Depth: num [1:16] 327 103 57 108 54 209 111 224 62 87.5 ...
  ..$ July_2008_Area : num [1:16] 86935 2774 1436 3630 2075 ...
  ..$ May_2009_Depth : num [1:16] 316 92 60 85 37 ...
  ..$ May_2009_Area  : num [1:16] 75548 3164 1827 2264 2580 ...

David Winsemius

unread,
Dec 8, 2009, 10:11:15 AM12/8/09
to Mark Na, manip...@googlegroups.com

On Dec 8, 2009, at 8:40 AM, Mark Na wrote:

> Hi,
>
> I have the following code, to write multiple dataframes to separate
> Excel worksheets:
>
> #make a list of the above dataframes, for llply processing (see
> below for str(datalist) output)
> datalist<-list(data1,data2,data3,data4,data5,data6)
> names(datalist)<-paste("data", 1:6, sep="")
>
> #then I have some code that uses llply to apply functions to each of
> the list elements (deleted here for brevity)
>
> #now, write each list element to an Excel worksheet (adapted from
> the Learning R blog at http://bit.ly/103oaP)
> save2excel <- function(x) sqlSave(xlsFile,
> x, tablename = names(datalist), rownames = FALSE)
> xlsFile<-odbcConnectExcel("results.xls",readOnly=FALSE)
> l_ply(datalist,save2excel)
> odbcCloseAll()
>
>
> I need to adapt the argument that names the Excel worksheets (which
> is now "tablename = names(datalist)") so it names each worksheet
> according the name of the list element.
>

The first think to clarify is whether you are using the term
"worksheet" to refer to what MS calls "workbook". If that is the case,
then you should modify your code to open and close a connection for
each work book file separately and not try to assign the entire 6
element vector to the table name.

If you are trying to address how to create a multi-sheet workbook, I
can be of no help.

--
David
> --
>
> You received this message because you are subscribed to the Google
> Groups "manipulatr" group.
> To post to this group, send email to manip...@googlegroups.com.
> To unsubscribe from this group, send email to manipulatr+...@googlegroups.com
> .
> For more options, visit this group at http://groups.google.com/group/manipulatr?hl=en
> .

David Winsemius, MD
Heritage Laboratories
West Hartford, CT

Mark Na

unread,
Dec 8, 2009, 10:48:23 AM12/8/09
to David Winsemius, manip...@googlegroups.com
Hi David. Thanks for your reply. Yes, I'm trying to create a multi-sheet (worksheet, in official MS parlance) workbook. The trouble is assigning the names of the table elements to the worksheets. That's the job of the tablename argument in the sqlSave function, but I can't figure out how to pass the name of each table element to that argument. If it was a loop, I could do it with indices, but I'm using l_ply and I don't understand how to replicate the functionality of indices with this function. Thanks for your input, Mark
--
Mark Na
University of Saskatchewan
Saskatoon, Canada

David Winsemius

unread,
Dec 8, 2009, 10:59:59 AM12/8/09
to Mark Na, manip...@googlegroups.com
For Pete's sake, man, if you know how to do it in a loop, then ... do it in a loop.

-- 
David

JiHO

unread,
Dec 8, 2009, 11:03:35 AM12/8/09
to David Winsemius, Mark Na, manip...@googlegroups.com
On Tue, Dec 8, 2009 at 16:59, David Winsemius <dwins...@comcast.net> wrote:
> For Pete's sake, man, if you know how to do it in a loop, then ... do it in
> a loop.

I think that, for this particular example, a loop would indeed not be
too harmful, because the most time consuming part will probably be the
IO to disk, so you won't gain much by using plyr (except for the nice
progress bar of course ;) )

JiHO
---
http://maururu.net

Learning R Blog

unread,
Dec 9, 2009, 1:18:02 AM12/9/09
to manipulatr
This should work, although the use of m_ply is not the most intuitive
option.

datalist<-list(iris,mtcars)
names(datalist)<-c("iris","mtcars")

save2excel <- function(data, tablename) sqlSave(xlsFile, data,
tablename, rownames = FALSE)

xlsFile<-odbcConnectExcel("results.xls",readOnly=FALSE)
m_ply(cbind(data = datalist, tablename = names(datalist)), save2excel)
odbcCloseAll()

--
http://learnr.wordpress.com

On Dec 8, 5:40 pm, Mark Na <mtb...@gmail.com> wrote:
> Hi,
>
> I have the following code, to write multiple dataframes to separate Excel
> worksheets:
>
> #make a list of the above dataframes, for llply processing (see below for
> str(datalist) output)
> datalist<-list(data1,data2,data3,data4,data5,data6)
> names(datalist)<-paste("data", 1:6, sep="")
>
> #then I have some code that uses llply to apply functions to each of the
> list elements (deleted here for brevity)
>
> #now, write each list element to an Excel worksheet (adapted from the
> Learning R blog athttp://bit.ly/103oaP)

Mark Na

unread,
Dec 11, 2009, 11:37:19 AM12/11/09
to Learning R Blog, manip...@googlegroups.com
Hello rlerarnr,

I am trying to adapt your solution for another purpose. I still have a table, the elements of which are dataframes. Now, I'd like to save each dataframe as a csv file. So, my code is:

write2csv<-function(data,file) write.csv(data,file)
m_ply(cbind(data=datalist,file=names(datalist)),write2csv)

This sort of works, but the files produced do not have the ".csv" extension. I don't know how to specify that the extension should be "csv".

If you can help, I'd really appreciate it.

Thanks, Mark







--

You received this message because you are subscribed to the Google Groups "manipulatr" group.
To post to this group, send email to manip...@googlegroups.com.
To unsubscribe from this group, send email to manipulatr+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/manipulatr?hl=en.


David Winsemius

unread,
Dec 11, 2009, 12:42:27 PM12/11/09
to Mark Na, Learning R Blog, manip...@googlegroups.com
On Dec 11, 2009, at 11:37 AM, Mark Na wrote:

Hello rlerarnr,

I am trying to adapt your solution for another purpose. I still have a table, the elements of which are dataframes. Now, I'd like to save each dataframe as a csv file. So, my code is:

write2csv<-function(data,file) write.csv(data,file)
m_ply(cbind(data=datalist,file=names(datalist)),write2csv)

This sort of works, but the files produced do not have the ".csv" extension. I don't know how to specify that the extension should be "csv".

?paste

Brendan O'Connor

unread,
Dec 11, 2009, 12:50:11 PM12/11/09
to David Winsemius, Mark Na, Learning R Blog, manip...@googlegroups.com
On Fri, Dec 11, 2009 at 12:42 PM, David Winsemius
<dwins...@comcast.net> wrote:
> On Dec 11, 2009, at 11:37 AM, Mark Na wrote:
> > write2csv<-function(data,file) write.csv(data,file)
> > m_ply(cbind(data=datalist,file=names(datalist)),write2csv)
>
> > This sort of works, but the files produced do not have the ".csv" extension. I don't know how to specify that the extension should be "csv".
>
> ?paste

and/or, ?sprintf

Brendan

--
http://anyall.org - Brendan O'Connor
language technologies institute, carnegie mellon university

Marek

unread,
Jan 18, 2010, 10:44:26 AM1/18/10
to manipulatr
Last time I have similar problem and end with below (adapted to your
problem):

l_ply(names(datalist), function(c_name, c_data=datalist[[c_name]]) { #
c_ states for current
write2csv(c_data, file=paste(c_name,".csv",sep="")) # with
workaround for extension
})

But I wonder which solution is more efficient in CPU time and RAM
uses. I usually play with big data so RAM is more important to me.

On other hand when I was looking for a solution first I tried standard
method to get names:

l_ply(datalist, function(c_data, c_name=deparse(substitute(c_data)))
print(c_name))

but I get

[1] ".data[[i]]"
[1] ".data[[i]]"
[1] ".data[[i]]"

So the question is: is there any way to get actual name? (to notice -
in lapply you get "X[[3L]]")
Maybe it is possible to get value of i, then one could use names
(data.list)[i]. I tried mess with parent.frame(-1) but I fail.

On 11 Gru 2009, 17:37, Mark Na <mtb...@gmail.com> wrote:
> Hello rlerarnr,
>
> I am trying to adapt your solution for another purpose. I still have a
> table, the elements of which are dataframes. Now, I'd like to save each
> dataframe as a csv file. So, my code is:
>
> write2csv<-function(data,file) write.csv(data,file)
> m_ply(cbind(data=datalist,file=names(datalist)),write2csv)
>
> This sort of works, but the files produced do not have the ".csv" extension.
> I don't know how to specify that the extension should be "csv".
>
> If you can help, I'd really appreciate it.
>
> Thanks, Mark
>

> > manipulatr+...@googlegroups.com<manipulatr%2Bunsu...@googlegroups.com>

Reply all
Reply to author
Forward
0 new messages