[R] Factor vs character in a data.frame vs vector

Marc Schwartz marc_schwartz at me.com
Fri Jul 7 16:37:12 CEST 2017


> On Jul 7, 2017, at 6:03 AM, John Kane via R-help <r-help at r-project.org> wrote:
> 
> This is not  serious problem but I just wonder if someone can explain what is happening.
> The same command within a dataframe is giving me a factor and as a plain vector is giving me a character.  It's probably something simple that I have read and forgotten but I thought I'd ask.
> Thanks
> 
> #================================================
> dat1 <- data.frame(aa = letters[1:10])
> str(dat1)
> data.frame':    10 obs. of  1 variable:
> $ aa....letters.1.10.: Factor w/ 10 levels "a","b","c","d",..: 1 2 3 4 5 6 7 8 9 10#=============================================================
> bb = letters[1:10]
> str(bb)
> chr [1:10] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j"
> #==============================================================
> 


See the 'stringsAsFactors' argument in ?data.frame.

dat1 <- data.frame(aa = letters[1:10], stringsAsFactors = FALSE)

> str(dat1)
'data.frame':	10 obs. of  1 variable:
 $ aa: chr  "a" "b" "c" "d" ...


Regards,

Marc Schwartz



More information about the R-help mailing list