[R] Concatenate a Variable

Peter Dalgaard P.Dalgaard at biostat.ku.dk
Fri Feb 1 16:18:59 CET 2008


Carla Rebelo wrote:
> Good morning!
>
> I do not speak English very well and so I will try to explain the best I 
> can. I have this:
>
>  > tabela[,1]
> [1] a a b b a c b a c c c c c
> Levels: a b c
>
>  >unique(tabela[,1])
> [1] a b c
> Levels: a b c
>
>  >var<-unique(tabela[,1])[1]
>
>  > var
> [1] a
> Levels: a b c
>
> But if I concatenate like this
>  > cat("VAR: ", var, "\n")
>
> I obtain
>  >VAR:  1
>
> and I want to obtain
>  >VAR: a
>
> How can I do this? Thanks!
>
>   
The devil is in the Details .... -section of help(cat):

     >>>Other types of R object should be converted
     (e.g. by 'as.character' or 'format') before being passed to 'cat'. <<<

> cat("VAR: ", factor("a"), "\n")
VAR:  1
> cat("VAR: ", format(factor("a")), "\n")
VAR:  a
> cat("VAR: ", as.character(factor("a")), "\n")
VAR:  a


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


-- 
   O__  ---- Peter Dalgaard             Øster Farimagsgade 5, Entr.B
  c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
 (*) \(*) -- University of Copenhagen   Denmark          Ph:  (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)                  FAX: (+45) 35327907



More information about the R-help mailing list