1

I have some data that looks like this:

head(data)
   net1re net2re net3re net4re net5re net6re
24      3      2      1      2      3      3
33      1      1      1      1      1      2
30      3      1      1      1      1      3
22      2      1      1      1      1      1
31      3      2      1      1      1      2
1       2      1      1      1      1      2

I'm running principal component analysis as follows:

library(psych)
fit <- principal(data[,1:6], rotate="varimax")
data$friendship=fit$scores

This creates the variable "friendship" which I can call on the console:

> colnames(data)
[1] "net1re"     "net2re"     "net3re"     "net4re"     "net5re"    
[6] "net6re"     "friendship"

But when I want to view my data, instead of the variable name I get "PC1":

> head(data)
   net1re net2re net3re net4re net5re net6re         PC1
24      3      2      1      2      3      3  1.29231531
33      1      1      1      1      1      2 -0.68448111
30      3      1      1      1      1      3  0.02783916
22      2      1      1      1      1      1 -0.67371031
31      3      2      1      1      1      2  0.10251282
1       2      1      1      1      1      2 -0.44345075

This becomes a major trouble because I need to repeat that with diffrent variables and all the results get "PC1".

Why is this happening and how can I assign the variable name instead of "PC1".

Thanks

1 Answer 1

2

This unusual effect appears becausefit$scores is a matrix:

str(data)
#'data.frame':  6 obs. of  7 variables:
# $ net1re    : int  3 1 3 2 3 2
# $ net2re    : int  2 1 1 1 2 1
# $ net3re    : int  1 1 1 1 1 1
# $ net4re    : int  2 1 1 1 1 1
# $ net5re    : int  3 1 1 1 1 1
# $ net6re    : int  3 2 3 1 2 2
# $ friendship: num [1:6, 1] 1.1664 -1.261 0.0946 -0.5832 1.1664 ...
#  ..- attr(*, "dimnames")=List of 2
#  .. ..$ : chr  "24" "33" "30" "22" ...
#  .. ..$ : chr "PC1"

To get the desired result, you can use

data$friendship=as.vector(fit$scores)

or

data$friendship=fit$scores[,1]

In either case, the output will be:

data
#   net1re net2re net3re net4re net5re net6re  friendship
#24      3      2      1      2      3      3  1.16635312
#33      1      1      1      1      1      2 -1.26098965
#30      3      1      1      1      1      3  0.09463653

str(data)
#'data.frame':  6 obs. of  7 variables:
# $ net1re    : int  3 1 3 2 3 2
# $ net2re    : int  2 1 1 1 2 1
# $ net3re    : int  1 1 1 1 1 1
# $ net4re    : int  2 1 1 1 1 1
# $ net5re    : int  3 1 1 1 1 1
# $ net6re    : int  3 2 3 1 2 2
# $ friendship: num  1.1664 -1.261 0.0946 -0.5832 1.1664 ...
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.