[R] Cannot rescale a constant/zero column error.

Jason Fill jason at leadlytics.com
Fri Nov 2 15:27:47 CET 2012


Petr,

Thank you for your reply, I actually figured out the issue this morning, I needed to add more data to my data file and it resolved it.

Thanks again for everyones time!

Jason

On Nov 2, 2012, at 9:00 AM, PIKAL Petr wrote:

> Hi
> 
>> 
>> Jim,
>> 
>> Thank you for your reply.  I guess I am just not understanding what the
>> difference is.  If I run your example using the pcmatrix<-
>> data.frame(Q1=sample(1:5,10,TRUE),Q2=sample(1:5,10,TRUE)) and print out
>> pcmatrix I get:
>> 
>> 	Q1	Q2
>> 1	98	99
> 
> Thats rather surprising. I get
> 
>> pcmatrix<-data.frame(Q1=sample(1:5,10,TRUE),Q2=sample(1:5,10,TRUE))
>> pcmatrix
>   Q1 Q2
> 1   4  4
> 2   3  3
> 3   4  1
> 4   4  4
> 5   4  2
> 6....
> 
> this what I get
> 
> 
>> 
>> Then if I mirror my input file to be, would would be what the samples
>> from above would have created, it would look like this:
>> 
>> ID,Q1,Q2
>> 1,2,3
> 
> what about include 
> 
> dput(head(yourfile)) 
> to your post to let us see how it looks like. Or at least 
> 
> str(yourfile)
> 
> Regards
> Petr
> 
> 
>> 
>> And I run the script printing out pcmatrix I get the exact same thing.
>> 
>> 	Q1	Q2
>> 1	98	99
>> 
>> So I would think they are basically the same representations, however
>> one works when passed into the prcomp function and the other does not.
>> I am just trying to tweak a script that we had created for us (cannot
>> find the guy now) so I can pass more columns of data.  I clearly really
>> do not know what I am doing, but again thanks for you time and any
>> direction you can point me in would be awesome.
>> 
>> Thanks
>> 
>> Jason
>> 
>> On Nov 1, 2012, at 3:57 AM, Jim Lemon wrote:
>> 
>>> On 10/31/2012 11:47 PM, fillay89 wrote:
>>>> I am trying to run the R Script below, I have actually simplified it
>>>> to just this part that is causing issues.  When I run this script I
>>>> continue to get an error that says "cannot rescale a constant/zero
>>>> column to a unit variance".  I cannot figure out what is going on
>>>> here.  I have stripped down my data file so it is more manageable so
>> I can try to figure this out.
>>>> 
>>>> The data.txt file that is being read looks like this:
>>>> 
>>>> 
>>>> I have made this file very basic on purpose to see if I could get
>>>> this to work, but it is not working.  Of course once I get this to
>>>> actually work I will expand the data file to match the data I am
>> actually using.
>>>> 
>>>> 
>>>> 
>>>> If I change the attribute in the prcomp function to scale=FALSE of
>>>> course I can run my script.  But if it is scaling...which is causing
>>>> the issues, it errors.
>>>> 
>>>> Any help would be GREATLY appreciated.
>>>> 
>>> Hi fillay89,
>>> You seem to be trying to run a principal component analysis on two
>> numbers, which won't produce anything useful. If I make up some data
>> that is more realistic:
>>> 
>>> pcmatrix<-data.frame(Q1=sample(1:5,10,TRUE),Q2=sample(1:5,10,TRUE))
>>> pcmatrix[,1] <- pcmatrix[,1] + 96
>>> pcmatrix[,2] <- pcmatrix[,2] + 96
>>> x.pca <- prcomp(pcmatrix,retx=TRUE,center=TRUE,scale=TRUE,cor=TRUE)
>>> x.pca
>>> Standard deviations:
>>> [1] 1.263267 0.635733
>>> 
>>> Rotation:
>>>        PC1        PC2
>>> Q1 0.7071068 -0.7071068
>>> Q2 0.7071068  0.7071068
>>> 
>>> it runs and I get a slightly more useful result. Running your code
>> does bomb in exactly the way you describe, as does:
>>> 
>>> x.pca <- prcomp(data.frame(Q1=99,Q2=98), retx=TRUE, center=TRUE,
>>> scale=TRUE, cor=TRUE) Error in prcomp.default(data.frame(Q1 = 99, Q2
>> = 98), retx = TRUE, center = TRUE,  :
>>> cannot rescale a constant/zero column to unit variance
>>> 
>>> Your problem is that you have a data frame with only one value (i.e.
>> a constant) in each column.
>>> 
>>> Jim
>> 
>> ______________________________________________
>> 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.
> 
> ______________________________________________
> 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