Convert table to dataframe without factors

I wish to convert a table into a dataframe without converting character vectors to factors. However, the stringsAsFactors parameter doesn’t work as expected in this case.

#create table
(t <- table(c("a", "b"), c("c", "c")))   
#    c
#  a 1
#  b 1

#convert table into dataframe
(df <- data.frame(t, stringsAsFactors=F))
#  Var1 Var2 Freq
#1    a    c    1
#2    b    c    1

#df column is factor
[1] "factor"

>Solution :

You need instead, data.frame won’t modify the original factors:

> df <-, stringsAsFactors=F)
> class(df$Var1)
[1] "character"

Leave a Reply