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?
See>?data.frameIn 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.
Hi Ed, Because that's the default for data.frame(), and a lot of people trip over it just as you did. It's an easy fix: R> data(iris) R> iris <- data.frame(iris, annot=c(""), stringsAsFactors=FALSE) R> iris[1,"annot"]<-"annotation" Thanks for the very useful reproducible example. Sarah On Wed, Aug 28, 2013 at 4:56 PM, Ed Siefker <ebs15242 at gmail.com> wrote:> 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? >-- Sarah Goslee http://www.functionaldiversity.org
Hi, Try: ?iris1<-data.frame(iris,annot=c(""),stringsAsFactors=FALSE) str(iris1) #'data.frame':??? 150 obs. of? 6 variables: # $ Sepal.Length: num? 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ... # $ Sepal.Width : num? 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ... # $ Petal.Length: num? 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ... # $ Petal.Width : num? 0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ... # $ Species???? : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ... # $ annot?????? : chr? "" "" "" "" ... ?iris1[1,"annot"]<-"annotation" ?head(iris1,3) #? Sepal.Length Sepal.Width Petal.Length Petal.Width Species????? annot #1????????? 5.1???????? 3.5????????? 1.4???????? 0.2? setosa annotation #2????????? 4.9???????? 3.0????????? 1.4???????? 0.2? setosa?????????? #3????????? 4.7???????? 3.2????????? 1.3???????? 0.2? setosa?? A.K. ----- Original Message ----- From: Ed Siefker <ebs15242 at gmail.com> To: r-help at r-project.org Cc: Sent: Wednesday, August 28, 2013 4:56 PM 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.