[R] why is this a factor?

arun smartpink111 at yahoo.com
Wed Aug 28 23:19:07 CEST 2013


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.




More information about the R-help mailing list