[R] why is this a factor?

Nordlund, Dan (DSHS/RDA) NordlDJ at dshs.wa.gov
Wed Aug 28 23:08:23 CEST 2013


See

>?data.frame

In particular, the stringsAsFactors parameter which defaults to TRUE.


Hope this is helpful,

Dan

Daniel J. Nordlund
Washington State Department of Social and Health Services
Planning, Performance, and Accountability
Research and Data Analysis Division
Olympia, WA 98504-5204


> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of Ed Siefker
> Sent: Wednesday, August 28, 2013 1:57 PM
> To: r-help at r-project.org
> Subject: [R] why is this a factor?
> 
> I have a table, and I want a new column to add some annotations to.
> But it ends up as a factor instead of characters, and won't let me add
> arbitrary text.
> 
> > data(iris)
> > iris<-data.frame(iris,annot=c(""))
> > iris[1,"annot"]<-"annotation"
> Warning message:
> In `[<-.factor`(`*tmp*`, iseq, value = "annotation") :
>   invalid factor level, NAs generated
> > class(iris[,"annot"])
> [1] "factor"
> > class(c(""))
> [1] "character"
> 
> Why is c("") a character, but when I add it to a data frame it's a
> factor?
> What am I missing?  Is there a better way to add a new column to
> a data frame?
> 
> ______________________________________________
> 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.


More information about the R-help mailing list