[R] data.frame to character

Liaw, Andy andy_liaw at merck.com
Fri Jun 10 14:30:12 CEST 2005


Is this what you want?

> d[] <- lapply(d, as.character)
> str(d)
`data.frame':   10 obs. of  3 variables:
 $ x  : chr  "XYZ" "XYZ" "XYZ" "XYZ" ...
 $ y  : chr  "A" "B" "C" "D" ...
 $ fac: chr  "B" "A" "C" "B" ...

Andy

> From: Muhammad Subianto
> 
> Hi,
> Excuse me for this simple question.
> How to convert as.data.frame to as.character?
> 
>   ?data.frame
> 
>  > L3 <- LETTERS[1:3]
>  >  L10 <- LETTERS[1:10]
>  >  d <- data.frame(cbind(x=c("XYZ"), y=L10), fac=sample(L3, 
> 10, repl=TRUE))
>  >  d
>       x y fac
> 1  XYZ A   A
> 2  XYZ B   A
> 3  XYZ C   A
> 4  XYZ D   A
> 5  XYZ E   B
> 6  XYZ F   C
> 7  XYZ G   A
> 8  XYZ H   C
> 9  XYZ I   B
> 10 XYZ J   A
>  >  str(d)
> `data.frame':   10 obs. of  3 variables:
>   $ x  : Factor w/ 1 level "XYZ": 1 1 1 1 1 1 1 1 1 1
>   $ y  : Factor w/ 10 levels "A","B","C","D",..: 1 2 3 4 5 6 7 8 9 10
>   $ fac: Factor w/ 3 levels "A","B","C": 1 1 1 1 2 3 1 3 2 1
>  >  d[3,]
>      x y fac
> 3 XYZ C   A
>  >
>  >  as.character(d[3,])
> [1] "1" "3" "1"
>  >
> 
> I think as.character the result something like
> [3] "XYZ" "C" "A"
> 
> I don't know how to convert it.
> Any help gratefully received.
> Thank you very much in advance.
> Kindly regards,
> Muhammad Subianto
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! 
> http://www.R-project.org/posting-guide.html
> 
> 
>




More information about the R-help mailing list