Skip to content

Converting Factor to Vector

5 messages · Bill Venables, Gundala Viswanath, David Winsemius +1 more

#
Hi all,

How can I convert factor like this:
'data.frame':	1000 obs. of  1 variable:
 $ AAA: Factor w/ 1000 levels "AAT","AAC",..: 1 2 3 4 5 6 7 8 9 10 ...
AAA
1  AAA
2  AAT
3  AAC
...

into to simple vector
chr [1:100] "AAA" "AAT" "AAC" "AAG" "ATA" "ATT"...
[1] "AAA" "AAT" "AAC" "AAG" "ATA" "ATT"...


I tried as.vector(), but it remains the same factor.

- Gundala Viswanath
Jakarta - Indonesia
#
as.character() 


Bill Venables
http://www.cmis.csiro.au/bill.venables/ 


-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Gundala Viswanath
Sent: Tuesday, 13 January 2009 3:25 PM
To: r-help at stat.math.ethz.ch
Subject: [R] Converting Factor to Vector

Hi all,

How can I convert factor like this:
'data.frame':	1000 obs. of  1 variable:
 $ AAA: Factor w/ 1000 levels "AAT","AAC",..: 1 2 3 4 5 6 7 8 9 10 ...
AAA
1  AAA
2  AAT
3  AAC
...

into to simple vector
chr [1:100] "AAA" "AAT" "AAC" "AAG" "ATA" "ATT"...
[1] "AAA" "AAT" "AAC" "AAG" "ATA" "ATT"...


I tried as.vector(), but it remains the same factor.

- Gundala Viswanath
Jakarta - Indonesia

______________________________________________
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.
#
Hi Bill,

However with as.character it fail show the actual strings.

It gives this:
chr "1:32267"
[1] "1:32267"

Instead of
- Gundala Viswanath
Jakarta - Indonesia
On Tue, Jan 13, 2009 at 2:40 PM, <Bill.Venables at csiro.au> wrote:
#
But repo is a dataframe. AAA is a factor within repo. You probably  
need to apply as.character to repo$AAA.
#
Using 'str' to see what you have is excellent practice
(and very helpful to show in messages to R-help).
However, you need to look at the output you get
more carefully.  You have a data frame that contains
a factor.  So:

as.character(repo[, 1])

should do what you want.


Patrick Burns
patrick at burns-stat.com
+44 (0)20 8525 0696
http://www.burns-stat.com
(home of "The R Inferno" and "A Guide for the Unwilling S User")
Gundala Viswanath wrote: