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@duke.edu [[alternative HTML version deleted]]
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
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 vectorsSo 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