I have selected a few columns of my original dataset with the code:
envfriend <- data.l1[c("eb_1","eb_2","eb_33","eb_34","eb_35","eb_36","eb_transpo","eb_read","eb_notetaking","eb_beverage","eb_breakfast","eb_lunch","eb_dinner")]
When I then try to conduct a Principal Components Analysis on this new dataset (it's a data.set, not data.frame), I get the error:
Error in `dimnames<-`(`*tmp*`, value = dimnames) :
invalid 'dimnames' given for data set
I have tried to remove the names/dimnames, but that reveals a similar error. Why are my dimnames
invalid? How do I fix this?
I'm assuming you're using data.set
from the "memisc" package. If this is the case, you should be able to solve your problem just by wrapping your data.set
in as.data.frame
, as suggested under ?data.set
.
Here is an example using the USArrests dataset that comes with R. I've created a data.set
version and tried to run princomp
and get the error you mention.
This is how princomp
works on the original data.frame
.
princomp(USArrests, cor = TRUE)
# Call:
# princomp(x = USArrests, cor = TRUE)
#
# Standard deviations:
# Comp.1 Comp.2 Comp.3 Comp.4
# 1.5748783 0.9948694 0.5971291 0.4164494
#
# 4 variables and 50 observations.
Now, let's create a data.set
version:
library(memisc)
ARRESTS <- data.set(USArrests)
rownames(ARRESTS) <- rownames(USArrests)
This is where you get your error:
princomp(ARRESTS, cor = TRUE)
# Error in `dimnames<-`(`*tmp*`, value = dimnames) :
# invalid 'dimnames' given for data set
And this is how you can try to solve it:
princomp(as.data.frame(ARRESTS), cor = TRUE)
# Call:
# princomp(x = data.frame(ARRESTS), cor = TRUE)
#
# Standard deviations:
# Comp.1 Comp.2 Comp.3 Comp.4
# 1.5748783 0.9948694 0.5971291 0.4164494
#
# 4 variables and 50 observations.
Note from the above that I was able to modify the rownames
of the data.set
, while you mentioned you were getting similar errors in those cases. My guess is that you were trying to set them to NULL
, which wouldn't work. From the "Note" section at ?dimnames
:
Setting components of the dimnames, e.g.,
dimnames(A)[[1]] <- value
is a common paradigm, but note that it will not work if the value assigned isNULL
. Userownames
instead, or (as it does) manipulate the whole dimnames list.