Re: [R] Assigning a larger number of levels to a factor that has fewer levels

From: Dimitri Liakhovitski <dimitri.liakhovitski_at_gmail.com>
Date: Thu, 07 Apr 2011 12:57:32 -0400

Thanks a lot, Ista!

On Thu, Apr 7, 2011 at 11:52 AM, Ista Zahn <izahn_at_psych.rochester.edu> wrote:
> Hi Dimitri,
> The factor() function allows you to specify the levels. So
>
> small.frame$myfactor <- factor(small.frame$myfactor, levels =
> levels(large.frame$myfactor))
>
> should do it.
>
> Best,
> Ista
>
> On Thu, Apr 7, 2011 at 11:47 AM, Dimitri Liakhovitski
> <dimitri.liakhovitski_at_gmail.com> wrote:
>> Hello!
>>
>> I have larger and a smaller data frame with 1 factor in each - it's
>> the same factor:
>>
>> large.frame<-data.frame(myfactor=LETTERS[1:10])
>> small.frame<-data.frame(myfactor=LETTERS[c(9,7,5,3,1)])
>> levels(large.frame$myfactor)
>> levels(small.frame$myfactor)
>> table(large.frame$myfactor)
>> table(small.frame$myfactor)
>>
>> myfactor has 10 levels in large.frame and 5 levels in small.frame. All
>> 5 levels in small.frame are present in large.frame.
>> How could I make levels(small.frame$myfactor) contain all the same
>> levels as levels(large.frame$myfactor)?
>> In other words, I want table(small.frame$myfactor) to have the same
>> number of entries as table(small.frame$myfactor) but with 5 zeros in
>> it:
>> A B C D E F G J I J
>> 1 0 1 0 1 0 1 0 1 0
>>
>> Thank you very much for your suggestions!
>> --
>> Dimitri Liakhovitski
>> Ninah Consulting
>> www.ninah.com
>>
>> ______________________________________________
>> 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.
>>
>
>
>
> --
> Ista Zahn
> Graduate student
> University of Rochester
> Department of Clinical and Social Psychology
> http://yourpsyche.org
>

-- 
Dimitri Liakhovitski
Ninah Consulting
www.ninah.com

______________________________________________
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.
Received on Thu 07 Apr 2011 - 17:00:49 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 07 Apr 2011 - 17:10:29 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive