Reputation: 97
Here's the data I'm using (it's a high-dimensional data set and available within the psych package):
install.packages("psych")
data(sat.act)
I'm trying to clean up the data a bit more so that I can run a factor analysis on it. Here's my code so far:
data1 <- data[,6:700]
satact <- data.frame(data1)
satact1 <- na.omit(data1)
dim(data)
But I'm getting an error stating there is an incorrect number of dimensions for my data[,6,700]. I have tried all possible combinations I can think of, and am not sure why it's showing up as incorrect. Any help will be greatly appreciated!
Upvotes: 0
Views: 92337
Reputation: 11
I got the same error, that is, an incorrect number of dimensions. I created the matrix as below and I had given the dimension dim as c(x,x,x)
ary <- array(seq(from=-98, to=100, by=4), dim= c(5,5,1))
While accessing it as below, it threw the "incorrect number of dimensions" error
ary[1,] &ary[,1] & ary[1,1]
then I recreated the matrix again as below and I gave the dimension dim as c(x,x)
ary <- array(seq(from=-98, to=100, by=4), dim= c(5,5))
Now the error is gone and we can access the array as usual
Upvotes: 1
Reputation: 6193
Elle, try this if you want to exclude the records with NA
.
library(psych)
data <- sat.act[complete.cases(sat.act), ]
prcomp(data)
Standard deviations:
[1] 146.8300134 68.2543504 9.5430803 3.6666452 1.1715551 0.4647055
Rotation:
PC1 PC2 PC3 PC4 PC5
gender 0.0003401435 -0.0012020541 0.0010970565 0.005788342 -0.0591625722
education -0.0004299901 -0.0002918314 -0.0850528466 0.024064095 -0.9943380906
age 0.0027020833 0.0014188342 -0.9929359565 -0.084983862 0.0824906340
ACT -0.0208448308 0.0016963799 -0.0827031922 0.995852246 0.0314096125
SATV -0.6956211490 -0.7182796666 -0.0017374908 -0.013494765 0.0003648618
SATQ -0.7181010432 0.6957498829 0.0003989952 -0.016166443 -0.0003874180
PC6
gender -0.9982301944
education 0.0589781669
age -0.0064738244
ACT 0.0038129483
SATV 0.0005261265
SATQ -0.0011528452
Or if you want to force NA
to 0
data <- sat.act
data[is.na(data)] <- 0
prcomp(data)
Standard deviations:
[1] 159.4488983 85.1587086 9.5463091 3.7961644 1.1814762 0.4653497
Rotation:
PC1 PC2 PC3 PC4 PC5
gender 0.0003915730 -7.364935e-04 0.0008193646 0.002717142 -0.0591610356
education -0.0004932616 -9.314099e-05 -0.0837084272 0.019199014 -0.9945610838
age 0.0012746540 4.606768e-03 -0.9933615141 -0.080624581 0.0817016560
ACT -0.0172578373 -1.064616e-02 -0.0788111515 0.996345023 0.0259420620
SATV -0.5500283967 -8.349310e-01 -0.0030404778 -0.018696325 0.0002655931
SATQ -0.8349664116 5.502319e-01 0.0021652104 -0.008410428 -0.0000266278
PC6
gender -0.9982440693
education 0.0589261882
age -0.0058797665
ACT 0.0011109106
SATV 0.0003311219
SATQ -0.0007530176
Upvotes: 2