[R] Error for atomic vectors and integers issue

peter dalgaard pdalgd at gmail.com
Sun Sep 29 11:51:38 CEST 2013


On Sep 29, 2013, at 02: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

So iris.bind does not accept $-operations. Apparently, you think it is a data frame, but try str(iris.bind) and see what it really is (a matrix, I'd expect). You might want to try

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

or maybe

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

Again, check with str() to see what data structure is produced, which names, etc.


> 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.

-- 
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Email: pd.mes at cbs.dk  Priv: PDalgd at gmail.com



More information about the R-help mailing list