Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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
class(df$Var1)
[1] "factor"

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

> df <- as.data.frame(t, stringsAsFactors=F)
> class(df$Var1)
[1] "character"
> 
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading