[19145] in s-news-athena

home help back first fref pref prev next nref lref last post

Re: [S] Changing factors to character vectors in data frames

daemon@ATHENA.MIT.EDU (Marcelo A. Ferreira Gomes)
Wed Sep 8 13:06:32 1999

Message-Id: <37D69596.CD5CB7C0@trip.com.br>
Date: Wed, 08 Sep 1999 13:58:02 -0300
From: "Marcelo A. Ferreira Gomes" <mgomes@trip.com.br>
Mime-Version: 1.0
To: s-news@wubios.wustl.edu
Content-Type: text/plain; charset=us-ascii; x-mac-type="54455854"; x-mac-creator="4D4F5353"
Content-Transfer-Encoding: 7bit


Michael Conklin wrote:

> I am experimenting with OLE automation and would like to put a data
> frame into excel. Unfortunately, the data is character data which gets
> coerced to factors when the data frame gets made. The factors get put
> into Excel as their numeric level so I get a worksheet full of 1's and
> 2's instead of the actual data content.
>
> Is there any way to prevent as.data.frame or data.frame from coercing
> to factors. (The help file says using the I() function works but I have
> not had any success with it). I am using Splus 2000 on NT 4.0 sp4.

Yes, there are some ways around it.

You can use the "as.is" option when you create your data frame.

But if the data frame is created by a function written by others and you
wouldn't like to mess around with it, you can try:

my.char.array <- as.character(your.data.frame[,column.with.chars])

Then you can do whatever you like with my.char.array, including the
use of it to come up with another data frame to be exported to Excel.
But this time, don't forget to set the "as.is" option to TRUE at least
for the character data column.

Something else to consider (and this may result in a smaller Excel
file, depending on the size of your data and your character strings)
is to use your exported data as you have gotten (the numeric levels)
and to also export the character levels represented by them using

levels(your.data.frame[,column.with.chars]).

In the resulting spreadsheet, you could use the Excel HLookup
or VLookup funcions to recover the original character data in
any summary or ghaph you need to make, instead of having the
character strings themselves replicated several times.

Of course, all this is really practical if you are interested
on OLE automation. If all you want is to export data to Excel,
I guess that the easiest way is to

write.table(your.data.frame, filename, sep="\t");

and then read in the resulting tab-separated ASCII file to Excel.

--
Marcelo A. Ferreira Gomes <mailto:mgomes@trip.com.br>
Systems Analist & CS consultant
Rio de Janeiro, RJ, Brasil


-----------------------------------------------------------------------
This message was distributed by s-news@wubios.wustl.edu.  To unsubscribe
send e-mail to s-news-request@wubios.wustl.edu with the BODY of the
message:  unsubscribe s-news

home help back first fref pref prev next nref lref last post