level sets of factors are different

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

level sets of factors are different

Richard van Wingerden
Hi,

I have two data frames A en B.
I want to filter B with values of A

data_frame_b
names <- colnames(data_frame_b[1:1, ])

filtera <- data.frame(data_frame_a[1:1])
       
        print(nrow(filtera))
       
        if (nrow(filtera)>0){
               
                filtered_frame_b <- subset(data_frame_b, ColumnX == filtera[1, 1], names)
         }
The results are:

[1] 124
Error in Ops.factor(ColumnX, filtera[1, 1]) :
        level sets of factors are different


What is wrong??

Richard

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: level sets of factors are different

Prof Brian Ripley
The message is really explicit.

You are trying to compare the equality of two factors with different level
sets.  Such factors are not comparable (and this is discussed on ?factor).
You didn't give a reproducible example, and you have not told us what you
are trying to do, so all we can do is repeat R's report that what you
actually did is not sensible.

On Sat, 7 Jan 2006, Richard van Wingerden wrote:

> Hi,
>
> I have two data frames A en B.
> I want to filter B with values of A
>
> data_frame_b
> names <- colnames(data_frame_b[1:1, ])
>
> filtera <- data.frame(data_frame_a[1:1])

This is bit strange. data_frame_a[1:1] is data_frame_a[1] and is a
single-column data-frame.  I think you might just as well use

filtera <- data_frame_a[[1]]

which is (probably) a factor.  You then seem to want to extract values
equal to its first element, so maybe you actually wanted
columnX == as.character(data_frame_a[1,1]) ?

>
> print(nrow(filtera))
>
> if (nrow(filtera)>0){
>
> filtered_frame_b <- subset(data_frame_b, ColumnX == filtera[1, 1], names)
>         }
> The results are:
>
> [1] 124
> Error in Ops.factor(ColumnX, filtera[1, 1]) :
>        level sets of factors are different
>
>
> What is wrong??
>
> Richard


--
Brian D. Ripley,                  [hidden email]
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: level sets of factors are different

Richard van Wingerden
Thanks!

The as.character fixed it!

Regards,
Richard

On 1/7/06, Prof Brian Ripley <[hidden email]> wrote:

> The message is really explicit.
>
> You are trying to compare the equality of two factors with different level
> sets.  Such factors are not comparable (and this is discussed on ?factor).
> You didn't give a reproducible example, and you have not told us what you
> are trying to do, so all we can do is repeat R's report that what you
> actually did is not sensible.
>
> On Sat, 7 Jan 2006, Richard van Wingerden wrote:
>
> > Hi,
> >
> > I have two data frames A en B.
> > I want to filter B with values of A
> >
> > data_frame_b
> > names <- colnames(data_frame_b[1:1, ])
> >
> > filtera <- data.frame(data_frame_a[1:1])
>
> This is bit strange. data_frame_a[1:1] is data_frame_a[1] and is a
> single-column data-frame.  I think you might just as well use
>
> filtera <- data_frame_a[[1]]
>
> which is (probably) a factor.  You then seem to want to extract values
> equal to its first element, so maybe you actually wanted
> columnX == as.character(data_frame_a[1,1]) ?
>
> >
> >       print(nrow(filtera))
> >
> >       if (nrow(filtera)>0){
> >
> >               filtered_frame_b <- subset(data_frame_b, ColumnX == filtera[1, 1], names)
> >         }
> > The results are:
> >
> > [1] 124
> > Error in Ops.factor(ColumnX, filtera[1, 1]) :
> >        level sets of factors are different
> >
> >
> > What is wrong??
> >
> > Richard
>
>
> --
> Brian D. Ripley,                  [hidden email]
> Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
> University of Oxford,             Tel:  +44 1865 272861 (self)
> 1 South Parks Road,                     +44 1865 272866 (PA)
> Oxford OX1 3TG, UK                Fax:  +44 1865 272595
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html