[R] labels to values

Matthias Wendel office at matthiaswendel.de
Thu Jan 10 12:48:38 CET 2008


 
Hello, Henrique,
	thank you for the tip, but it was not quite what has been desired:
> d[, 'Y6']
  [1]  6  3  8 11  8  9  6  8  3  5 10 15 NA  9  8  3  8 16  6  6 NA 10  5  2  7  7  6 16  7 15  7 10 12  8  7 12 12 16  7  6  8  8
15  6 NA  8 99  7 12  8  9 16  7 16  8  7  7  1 15
 [60] 12  8  7 10  7  8  7  8  9  8  6  6  8  6 16 11  5 11 11  1 11  3  7  7 10 10 10  6 11 16 NA  1  3  2 10 99 10  3  3  9  7 16
99 16  1 10  2 13 13 13 13 13 13 13 13 13 13 13 13
[119] 13 13 13 13 13 NA 10 16 16 NA  6 10  5 11 11  1  1  1  1 16  1 16  1  1  1  1  6  6  6 16  8 16 16 16 16  5  6 10 99 11 11 10
6  6  1  1  6  1 11 11 16  9 11 16  6  8  8 16 16
[178]  8  6 16 16 12 12 12 12 12 12 12 16  9 16 15 12 12 15 10 16 15  4  1  2 14  4  4  2  5 NA  1  5  5  7  9  5 12 12 NA 16 12 12
12 12 12 12 12 12 12 99 NA 12 12 NA  1 16  1  7 11
[237]  5  6  7  1 13  6  8 16  2  1  5 16 16  9  8  8  8  7 16  8  8  2  8  5  4  6 14  5 14  8  8 14  4  4  8 14  8 14  6  2  3 14
3 16  5 15 15 15 15 15 15 15 15 15 15 15 13 13 13
[296] 13 13 13 13 13 13 13 13 13 13 15  6 NA 12  3  9  9 NA 10 16
attr(,"value.labels")
                                n.a.                           Verwaltung Servicegesellschaft Waldfriede (SKW)
Kurzzeitpflege Waldfriede 
                                  99                                   16                                   15
14 
                       Sozialstation                  Krankenpflegeschule              Med. Technischer Dienst
Pflege OP 
                                  13                                   12                                   11
10 
                     Funktionsdienst                   Pflege Gynäkologie                     Pflege Chirurgie
Pflege Innere 
                                   9                                    8                                    7
6 
           Ärzte Anästhesie, Röntgen                    Ärzte Gynäkologie                      Ärzte Chirurgie
Ärzte Innere 
                                   5                                    4                                    3
2 
        Patientenberatung/-betreuung 
                                   1 
> x1 <- as.factor(d[,'Y6'])
> levels(x1)[levels(x1) %in% attributes(x1)[1]] <- c(names(unlist(lapply(levels(x1),function(z)which(z==attributes(x1)[[1]])))))
Fehler in levels(x1)[levels(x1) %in% attributes(x1)[1]] <- c(names(unlist(lapply(levels(x1),  : 
  inkompatible Typen (von NULL nach character) in subassignment Typ fix
> 

Meanwhile, I solved the problem by takin another route: the data.frame d was imported my read.spss; read.spss imports the factors as
factors only if number of distinct labels is equal to number of distinct values. By assuring this (it was not the case for Y6), I've
got what I Want.
Regards,
Matthias

-----Ursprüngliche Nachricht-----
Von: Henrique Dallazuanna [mailto:wwwhsd at gmail.com] 
Gesendet: Mittwoch, 9. Januar 2008 17:17
An: Matthias Wendel
Cc: r-help at stat.math.ethz.ch
Betreff: Re: [R] labels to values

If I understand your question, you can do:

x1 <- as.factor(d[,'Y6'])
levels(x1)[levels(x1) %in% attributes(x)[1]] <- c(names(unlist(lapply(levels(x1),function(z)which(z==attributes(x)[[1]])))))

On 09/01/2008, Matthias Wendel <office at matthiaswendel.de> wrote:
> I couldn't find out how the following to solve:
> There is a column in a data.frame:
>
> > d[, 'Y6']
>   [1]  6  3  8 11  8  9  6  8  3  5 10 15 NA  9  8  3  8 16  6  6 NA 
> 10  5  2  7  7  6 16  7 15  7 10 12  8  7 12 12 16  7  6  8  8
> 15  6  [45] NA  8 99  7 12  8  9 16  7 16  8  7  7  1 15 12  8  7 10  
> 7  8  7  8  9  8  6  6  8  6 16 11  5 11 11  1 11  3  7  7 10 10 10  6 
> 11  [89] 16 NA  1  3  2 10 99 10  3  3  9  7 16 99 16  1 10  2 13 13 
> 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 NA 10 16
> 16 NA  6 10  5 11 [133] 11  1  1  1  1 16  1 16  1  1  1  1  6  6  6 
> 16  8 16 16 16 16  5  6 10 99 11 11 10  6  6  1  1  6  1 11 11
> 16  9 11 16  6  8  8 16 [177] 16  8  6 16 16 12 12 12 12 12 12 12 16  
> 9 16 15 12 12 15 10 16 15  4  1  2 14  4  4  2  5 NA  1  5  5
> 7  9  5 12 12 NA 16 12 12 12 [221] 12 12 12 12 12 12 99 NA 12 12 NA  1 
> 16  1  7 11  5  6  7  1 13  6  8 16  2  1  5 16 16  9  8  8
> 8  7 16  8  8  2  8  5  4  6 14  5 [265] 14  8  8 14  4  4  8 14  8 14  
> 6  2  3 14  3 16  5 15 15 15 15 15 15 15 15 15 15 15 13 13
> 13 13 13 13 13 13 13 13 13 13 13 15  6 NA [309] 12  3  9  9 NA 10 16
> attr(,"value.labels")
>                           Verwaltung Servicegesellschaft Waldfriede (SKW)            Kurzzeitpflege Waldfriede
>                                   16                                   15                                   14
>                        Sozialstation                  Krankenpflegeschule              Med. Technischer Dienst
>                                   13                                   12                                   11
>                            Pflege OP                      Funktionsdienst                   Pflege Gynäkologie
>                                   10                                    9                                    8
>                     Pflege Chirurgie                        Pflege Innere            Ärzte Anästhesie, Röntgen
>                                    7                                    6                                    5
>                    Ärzte Gynäkologie                      Ärzte Chirurgie                         Ärzte Innere
>                                    4                                    3                                    2
>         Patientenberatung/-betreuung
>                                    1
>
> I'd like to have the column with the value.labels as values. How do I do that?
> Thanks,
> Matthias
>
> ______________________________________________
> 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.
>


--
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40" S 49° 16' 22" O




More information about the R-help mailing list