[R] Error for atomic vectors and integers issue

Prof Brian Ripley ripley at stats.ox.ac.uk
Sun Sep 29 11:43:48 CEST 2013


Please do read the help for cbind().  'iris' is a data frame, and you 
created a matrix.  Most likely you intended

iris.bind <- data.frame(x = iris.sub.pca$x, Species = iris$Species)


On 29/09/2013 01:06, Guy Wachsman wrote:
> Hi all,
>
> I was doing a PCA o the iris data set but when trying to rbind the "Species" column back from the original data set, it turned into 1:3 integers instead of the original ones. I noticed that the original names of the species are of type "integer", but I don't understand why, and how to put them back with the name. That might be important if you have more than 3 species. Then when I plotted I got an error message, here are the codes:
>
> works, but the species name has changed into 1,2,3:
>
> clrs <- c('red','green','blue')
> iris.sub=subset(iris, select=-Species)
> iris.sub.pca <- prcomp(iris.sub, center=T, retx=T)
> iris.bind=cbind(iris.sub.pca$x, iris$Species)
> plot(data=iris.bind, PC2~PC1, asp=1, pch=16, xlab='PC1',
> ylab='PC2',xlim=c(-5,5),ylim=c(-5,5), col = clrs[iris$Species] )
>
> If I plot like this:
>
> plot(iris.bind$x[,1], iris.bind$x[,2],asp=1,pch=16, xlab='PC1', ylab='PC2',xlim=c
>       (-5,5),ylim=c(-5,5), col = clrs[iris$Species] )
>
> I get this error message
>
> Error in iris.bind$x : $ operator is invalid for atomic vectors
>
> Thanks a lot,
> Guy
>
>
>
>
>
>
>
> --
> Guy Wachsman
> Benfey lab, FFSC #4131, Duke
> 130 Science Drive
> 27708, Durham, NC
> email: gw57 at duke.edu
>
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> 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.
>


-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595



More information about the R-help mailing list