s-news
[Top] [All Lists]

Re: [S] Warning message when rbinding

To: Ignacio Ramos <ira@hti-task.com>
Subject: Re: [S] Warning message when rbinding
From: John Wallace <jrw@pisces.fish.washington.edu>
Date: Tue, 23 Feb 1999 16:33:34 -0800 (PST)
Cc: s-news@wubios.wustl.edu
In-reply-to: <000d01be5f58$170ad820$1affa8c0@a>
Sender: owner-s-news@wubios.wustl.edu

This is a factor problem, if a column in the first data frame does not
have all the levels of the matching column in a subsequent data frame
then this error occurs.

I have also been struggling with this problem recently.  As luck would
have it, just today, I have found a solution.  (Note, another
approach would be to turn off the 'auto convert to factor' of the data
frames, as some people have done.)

The following function only handles two data frames at a time, but it
should solve your problem. 


rbind.df <- function(df1, df2)
{
#
#   DATE WRITTEN:  February 1999 
#   AUTHOR:  John R. Wallace (jrw@fish.washington.edu)
#
# 'which.fac' is needed to prevent other classes from being affected
# 
        which.fac <- (1:ncol(df1))[unlist(lapply(df1, is.factor))]
        out <- rbind(data.frame(lapply(df1, function(x)
        {
                if(is.factor(x))
                        I(as.character(x))
                else x
        }
        )), data.frame(lapply(df2, function(x)
        {
                if(is.factor(x))
                        I(as.character(x))
                else x
        }
        )))
        out[, which.fac] <- data.frame(lapply(out[, which.fac], 
                function(x)
        {
                class(x) <- NULL
                x
        }
        ))
        out
}


-----------

-- 
John Wallace
University of Washington                    ^    ^    ^
Fisheries Research Institute               / \  / \  / \   ^
Box 357980                                 / \  / \   |   / \
Seattle, WA 98195-7980                      |    |  o__~  / \
PHONE   (206) 543-1513                  @ @         /\/\   |
FAX     (206) 685-7471                   ~    
E-MAIL        jw@u.washington.edu              o
WWW      http://www.fish.washington.edu/people/jrw/Wallace.html 
                                                o  _///_ //
                                                <`)=  _<<
                                                    \\\  \\




On Tue, 23 Feb 1999, Ignacio Ramos wrote:

> 
> Greetings to all:
> I am using S-plus v4.5 for Windows 95. I'll try to explain my doubt quickly:
> I have 2 data frames (50000 x 150 & 3000 x 150) with the same number of
> columns and same column labels. The data in these columns is of almost every
> type. My "problem" is I get this warning message when I rbind them 3 times:
> 
>     Warning in "[<- .factor" (.A0,ri,value = .A1): replacement values not
> all in levels(x): NA's  generated.
> 
> I have made sure that the data type are the same pair by pair, i.e. I do
> not concatenate columns of different data type. My data frames have NA's in
> much more than 3 columns so it is not a NA problem. ri (look warning
> message) is not a variable name
> Can anyone tell me what that means?. Is it a problem a should take care of
> or is it just a bug because of the size of the data frames?. Thanks in
> advance.
> 
> Ignacio Ramos
> Escuela de Finanzas BBV - Data Mining
> mailto:ira@hti-task.com
> 
> -----------------------------------------------------------------------
> 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
> 


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

<Prev in Thread] Current Thread [Next in Thread>