[R] coarsening levels problem

arun smartpink111 at yahoo.com
Sat Nov 17 23:07:33 CET 2012


HI,
There were some "spaces" inside the quotes that created the problem.


 reg <-
structure(c(6L, 6L, 1L, 7L, 1L, 1L, 1L, 5L, 2L, 1L, 6L, 3L, 1L,
1L, 7L, 1L, 4L, 1L, 1L, 1L, 1L, 1L, 7L, 6L, 6L, 1L, 1L, 1L, 1L,
6L, 2L, 7L), .Label = c(" auckland", " christchurch", " hb",     
" manawatu", " taranaki", " waikato", " wellington"), class = "factor")
^^                     


I changed it to:
reg <-
structure(c(6L, 6L, 1L, 7L, 1L, 1L, 1L, 5L, 2L, 1L, 6L, 3L, 1L,
1L, 7L, 1L, 4L, 1L, 1L, 1L, 1L, 1L, 7L, 6L, 6L, 1L, 1L, 1L, 1L,
6L, 2L, 7L), .Label = c("auckland", "christchurch", "hb",
"manawatu", "taranaki", "waikato", "wellington"), class = "factor")levels(reg)<-rg2

 reg
# [1] other other akl   other akl   akl   akl   other other akl   other other
#[13] akl   akl   other akl   other akl   akl   akl   akl   akl   other other
#[25] other akl   akl   akl   akl   other other other
#Levels: akl other


A.K.

----- Original Message -----
From: Murray Jorgensen <maj at waikato.ac.nz>
To: r-help <r-help at r-project.org>
Cc: 
Sent: Saturday, November 17, 2012 4:28 PM
Subject: [R] coarsening levels problem

Greetings from New Zealand.

I hope that my difficulties are clear from the following output. Ideas gratefully received!

Murray Jorgensen

cc <- scan()
3000 3500 2500 2500 3000 3000 3000 3000 3000 2000
2000 2000 2000 4800 3000 4500 3000 2000 2000 4400
3000 3000 2500 3000 3000 2000 3000 2000 2000 2000 3000 2000

cc <- as.factor(cc)
> cc
[1] 3000 3500 2500 2500 3000 3000 3000 3000 3000 2000
[11] 2000 2000 2000 4800 3000 4500 3000 2000 2000 4400
[21] 3000 3000 2500 3000 3000 2000 3000 2000 2000 2000
[31] 3000 2000
Levels: 2000 2500 3000 3500 4400 4500 4800
> engroups <- list(L2 = 2000, L2.5 = 2500, L3 = 3000,
+                large = c(3500,4400,4500,4800))
> levels(cc) <- engroups
> cc
[1] L3    large L2.5  L2.5  L3    L3    L3    L3    L3
[10] L2    L2    L2    L2    large L3    large L3    L2
[19] L2    large L3    L3    L2.5  L3    L3    L2    L3
[28] L2    L2    L2    L3    L2
Levels: L2 L2.5 L3 large
reg <-
structure(c(6L, 6L, 1L, 7L, 1L, 1L, 1L, 5L, 2L, 1L, 6L, 3L, 1L,
1L, 7L, 1L, 4L, 1L, 1L, 1L, 1L, 1L, 7L, 6L, 6L, 1L, 1L, 1L, 1L,
6L, 2L, 7L), .Label = c(" auckland", " christchurch", " hb",
" manawatu", " taranaki", " waikato", " wellington"), class = "factor")
> reg
[1]  waikato       waikato       auckland
[4]  wellington    auckland      auckland
[7]  auckland      taranaki      christchurch
[10]  auckland      waikato       hb
[13]  auckland      auckland      wellington
[16]  auckland      manawatu      auckland
[19]  auckland      auckland      auckland
[22]  auckland      wellington    waikato
[25]  waikato       auckland      auckland
[28]  auckland      auckland      waikato
[31]  christchurch  wellington
7 Levels:  auckland  christchurch  hb ...  wellington
> rg2 <- list(akl = c("auckland"), other = c("christchurch",
+            "hb", "manawatu", "taranaki", "waikato", "wellington"))
> levels(reg) <- rg2
> reg
[1] <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA>
[11] <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA>
[21] <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA>
[31] <NA> <NA>
Levels: akl other


-- Dr Murray Jorgensen      http://www.stats.waikato.ac.nz/Staff/maj.html
Department of Statistics, University of Waikato, Hamilton, New Zealand
Email: maj at waikato.ac.nz    majorgensen at ihug.co.nz      Fax 7 838 4155
Phone  +64 7 838 4773 wk    Home +64 7 825 0441   Mobile 021 0200 8350

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





More information about the R-help mailing list