具有不同项目数的两个量表之间的相关性

时间:2017-12-03 22:51:28

标签: r correlation

我试图获得两个李克特量表之间的相关性:“理想”和“批准”。它们包括不同数量的物品。

>names(ideal)
[1] "IDEAL1"       "IDEAL2"       "IDEAL3"       "IDEAL4"       "IDEAL5"      
  "PROMOTIONAL6"
>str(ideal)
'data.frame':   96 obs. of  6 variables:
 $ IDEAL1      : int  4 5 3 5 5 3 2 6 4 3 ...
 $ IDEAL2      : int  3 3 3 3 2 2 3 5 4 3 ...
 $ IDEAL3      : int  4 2 3 4 3 2 4 3 4 3 ...
 $ IDEAL4      : int  4 5 3 4 4 2 3 5 5 3 ...
 $ IDEAL5      : int  3 5 3 4 3 2 4 5 5 3 ...
 $ PROMOTIONAL6: int  3 2 3 2 2 1 4 4 2 4 ...
> names(approval)
[1] "PROMOTIONAL3" "OUGHT2"       "OUGHTTO3"     "OUGHTTO4"    
> str(approval)
'data.frame':   96 obs. of  4 variables:
 $ PROMOTIONAL3: int  4 3 3 4 4 1 4 4 5 5 ...
 $ OUGHT2      : int  5 2 4 5 3 3 5 4 4 3 ...
 $ OUGHTTO3    : int  5 2 3 5 4 5 5 4 2 3 ...
 $ OUGHTTO4    : int  4 2 3 4 3 4 4 4 4 4 ...

我需要单个数量的相关性,而不是变量之间的多个相关性。因此,我将两个数据帧转换为向量并尝试获得一个相关系数:

cor.test(c(as.matrix(ideal)), c(as.matrix(approval)))

但是这个错误突然出现了:

Error in cor.test.default(c(as.matrix(ideal)), c(as.matrix(approval))) : 
'x' and 'y' must have the same length

有人可以帮忙吗?非常感谢!

1 个答案:

答案 0 :(得分:0)

  

我将两个数据帧转换为向量并尝试获得一个   相关系数

虽然这可能没什么意义,但这里有一个启动者:

ideal <- data.frame(
  IDEAL1 =c(4, 5, 3, 5, 5),
  IDEAL2 = c(3, 3, 3, 3, 2))
approval <- data.frame(
  PROMOTIONAL3 = c(4, 3, 3),
  OUGHT2 = c(5, 2, 4)
)
# data frames to vectors
vec1 <- unlist(ideal)
vec2 <- unlist(approval)
# determine common length
l <- min(length(vec1), length(vec2))
# determine cor coef
cor(vec1[1:l], vec2[1:l])
# [1] -0.09697623

vec1vec2有不同的长度,因此我(即你)需要缩短一个。