[19167] in s-news-athena

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

[S] follow-up: converting a list of dataframes into a single dataframe

daemon@ATHENA.MIT.EDU (John.Gavin@wdr.com)
Thu Sep 9 07:55:18 1999

From: John.Gavin@wdr.com
Date: Thu, 9 Sep 1999 12:49:59 +0100
Message-Id: <H00004ac0a5fb5fd@MHS>
Mime-Version: 1.0
To: s-news@wubios.wustl.edu
Content-Type: text/plain; charset=US-ASCII; name="BDY.TXT"
Content-Disposition: inline; filename="BDY.TXT"
Content-Transfer-Encoding: 7bit


Hi,

There is a follow-up to this posting 
about using  'do.call(rbind, ...)' to convert
a list of similar dataframes into single dataframe that
may be relevant to some (highlighted by Bill Dunlap).

In particular, the 'do.call(rbind, ...)' solution
does not do exactly what I claimed.

It transpires that 'rbind' demotes variables of class 
'ordered' to class 'factor'.
They have to be manually converted back.
e.g. 'class(variable.name) <- c("ordered","factor")' promotes them 
back 
from 'factor' to 'ordered' as required.

If there are a lot of variables in the dataframe then something like
the following might work (I haven't tested it extensively)

# from the original dataframe 'data' get the list of variable names 
# and decide if their class is 'ordered'
temp <- unlist(sapply(data, function(x) class(x)[1] )) == "ordered"
# remember those variables that should be of class 'ordered'
temp <- names(temp)[temp]
# reassign the class of those variables from factor to ordered
data.spread.df2 <- convert.col.type(data.spread.df, temp , "ordered")

Thanks again for the many responses to this question.

Regards,

John.

john.gavin@ubs.com

John Gavin
Quantitative Risk Models and Statistics
Warburg Dillon Read
1 Finsbury Ave (4th floor)
London EC2M 2PP, UK


> -----Original Message-----
> From: bill 
> Sent: 08 September 1999 21:43
> To: Gavin, John
> Cc: bill
> Subject: Re: [S] summary: converting a list of dataframes 
> into a single
> dataframe
> 
> 
> On Wed, 8 Sep 1999 John.Gavin@wdr.com wrote:
> 
> > There were two solutions.
> > The first did exactly what I wanted
> > 
> > 1) do.call("rbind",input)
> > 
> > 2) output <- NULL
> >    while(length(input) > 0)
> >    {
> > 	output <- rbind(output, input[[1]])
> > 	input[[1]] <- NULL
> >    }
> 
> I think rbind() still has a bug in it such that when it combines
> columns of class "ordered","factor" it produces on of class 
"factor".
> E.g.,
> 
> >  lapply(input, function(i)lapply(i,data.class))
> $a:
> $a$x:
> [1] "numeric"
> $a$y:
> [1] "factor"
> $a$z:
> [1] "ordered"
> $b:
> $b$x:
> [1] "numeric"
> $b$y:
> [1] "factor"
> $b$z:
> [1] "ordered"
> > lapply(do.call("rbind",input), data.class)
> $x:
> [1] "numeric"
> $y:
> [1] "factor"
> $z:
> [1] "factor"
> 
> Note the z component of the output should be "ordered".  As a
> workaround, you could process the output to assign the class
> c("ordered","factor") to the factors that you know to be ordered.
> 
> > Also each data frame may contain 'factor' and 'ordered' columns
> > so I want to preserve the original labels.
> 
> --------------------------------------------------------------
> --------------
> Bill Dunlap                                      22461 Mt 
> Vernon-Big Lake Rd
> Data Analysis Products Div. of MathSoft, Inc.    Mount 
> Vernon, WA 98274
> bill@statsci.com                                 360-428-8146


This message contains confidential information and is intended only 
for the individual named.  If you are not the named addressee you 
should not disseminate, distribute or copy this e-mail.  Please 
notify the sender immediately by e-mail if you have received this 
e-mail by mistake and delete this e-mail from your system.

E-mail transmission cannot be guaranteed to be secure or error-free 
as information could be intercepted, corrupted, lost, destroyed, 
arrive late or incomplete, or contain viruses.  The sender therefore 
does not accept liability for any errors or omissions in the contents 
of this message which arise as a result of e-mail transmission.  If 
verification is required please request a hard-copy version.  This 
message is provided for informational purposes and should not be 
construed as a solicitation or offer to buy or sell any securities or 
related financial instruments.

-----------------------------------------------------------------------
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