1

I am trying to use the following function here http://www.r-bloggers.com/using-r-two-plots-of-principal-component-analysis/

what I do is as follow:

data(iris) 
df <- iris[,1:4]
variable.groups <- c(rep(1,50), rep(2,50), rep(3,50))
library(reshape2)
library(ggplot2)
pca <- prcomp(df, scale=TRUE)
melted <- cbind(variable.groups, melt(pca$rotation[,1:4]))

I get an error saying

Error in data.frame (..., check.names=FALSE): arguments imply differing number of rows: 150, 16 

what I basically dont understand, is the melting in the above link, is there any idea, how i can set the iris data the same ?

  • `length(melt(pca$rotation[,1:4]))` is 16 and `length(variable.groups)` is 150. In the post you link, the length of both vectors are a match. `melt` basically transforms a wide data.frame into a long data.frame – scoa May 13 '15 at 06:43
  • @scoa that is true but do you have any idea how to fix variable.groups to be able to melt it ? how did I generate the variable.groups was based on different species and I thought that is the porpose of the example mentioned above . kinda confused now –  May 13 '15 at 06:59

1 Answers1

2

Quick answer:

variable.groups needs to be of length 4. So this fixes it:

variable.groups <- c(1,2,3,4) #or any other 4 classes

Long answer:

Comparing your code with the code at http://www.r-bloggers.com/using-r-two-plots-of-principal-component-analysis/ i think you are mixing sample.groups and variable.groups up.

Taking data from there article it has the following dimensions:

> dim(data)
[1] 50 70

So sample.groups and variable.groups do have the following length:

> length(sample.groups)
[1] 50
> length(variable.groups)
[1] 70

In your code df and variable.group do have the following dimensions:

> dim(df)
[1] 150   4
> length(variable.groups)
[1] 150

So compared to the code you are referencing your variable.groups should have length 4.

Rentrop
  • 20,979
  • 10
  • 72
  • 100