[R] Selectively swapping labels between factors

Charles C. Berry cberry at tajo.ucsd.edu
Sat Oct 27 21:48:47 CEST 2007

On Sat, 27 Oct 2007, Michael Kubovy wrote:

> Dear R-helpers,
> I'm trying to selectively swap labels between two factors, depending
> on an indicator variable i.
> Can you point me to a solution, and perhaps how I could have found it?
> labels(fact1) is a character vector of r row numbers

 	labels( object, ... )

is a generic function. See ?labels

I do not know of any method (e.g. 'methods( labels )' ) that returns a 
character vector of 'row numbers'. So, this is either your own method, a 
function that masks base:::labels(), or a misstatement about what labels() 

> levels(fact1) is a character vector of the n < r unique levels
> How do I then get the character vector of length r of the levels of
> fact1?

 	as.character( fact1 )

> Once I have that, is there anything else I need to know about
> swapping labels between factors in a df?

Would a 'df' be a data.frame object?

Do you mean swapping 'levels'? There is no 'labels' attribute of a factor 
in a data.frame or elsewhere.

If you mean to swap elements of two vectors that happen to be factors, the 
path of least resistance and confusion is likely to convert both to 
character vectors, perform the changes, then convert both to factors, and 
finally replace the orignals with the new versions.


> _____________________________
> Professor Michael Kubovy
> University of Virginia
> Department of Psychology
> USPS:     P.O.Box 400400    Charlottesville, VA 22904-4400
> Parcels:    Room 102        Gilmer Hall
>         McCormick Road    Charlottesville, VA 22903
> Office:    B011    +1-434-982-4729
> Lab:        B019    +1-434-982-4751
> Fax:        +1-434-982-4766
> WWW:    http://www.people.virginia.edu/~mk9y/
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

Charles C. Berry                            (858) 534-2098
                                             Dept of Family/Preventive Medicine
E mailto:cberry at tajo.ucsd.edu	            UC San Diego
http://famprevmed.ucsd.edu/faculty/cberry/  La Jolla, San Diego 92093-0901

More information about the R-help mailing list