开发者

R: Calculate correlation for a subset of my dataset?

I have a dataset with 20 variables v1 - v20. Now I'd like to use cor(...) to calculate the correlation between v2 and v10 开发者_运维知识库until v15 and v3 and v10 until v15. What's the best way to do this? Do I have to do this for each variable pair using

cor(v2, v10)
cor(v2, v11)
cor(v2, v12)
and so on?

Here is the actual dataset:

   > dput(dataset)
structure(list(Number = 1:15, Question.1.1 = c(3L, 4L, 5L, 5L, 
4L, 5L, 5L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L), Question.1.2 = c(1L, 
2L, 1L, 1L, 4L, 1L, 1L, 2L, 3L, 1L, 1L, 1L, 1L, 1L, 1L), Question.2.1 = c(5L, 
3L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L), Question.2.2 = c(2L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), Question.3.1 = c(2L, 
NA, 4L, 5L, 4L, 3L, 5L, 3L, 5L, 5L, 5L, 5L, 4L, 4L, 4L), Question.3.2 = c(2L, 
NA, 1L, 1L, 2L, 2L, 1L, 4L, 3L, 1L, 1L, 1L, 2L, 2L, 1L), Question.4.1 = c(3L, 
2L, 5L, 2L, 5L, 5L, 5L, 3L, 5L, 5L, 5L, 5L, 4L, 5L, 2L), Question.4.2 = c(2L, 
2L, 1L, 2L, 2L, 1L, 2L, 3L, 3L, 1L, 1L, 1L, 1L, 1L, 4L), Question.5.1 = c(5L, 
3L, 5L, 3L, 4L, 4L, 5L, 3L, 5L, 5L, 5L, 5L, 5L, 4L, 4L), Question.5.2 = c(2L, 
2L, 1L, 1L, 3L, 2L, 1L, 3L, 4L, 1L, 1L, 1L, 1L, 1L, 1L), Question.6.1 = c(5L, 
2L, 2L, 2L, 3L, 2L, 3L, 1L, 3L, 3L, 5L, 4L, 3L, 3L, 1L), Question.6.2 = c(2L, 
3L, 2L, 1L, 2L, 3L, 3L, 3L, 3L, 2L, 1L, 1L, 2L, 2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, 4L, 5L, 5L, 5L, 5L, 4L, 4L)), .Names = c("Number", 
"Question.1.1", "Question.1.2", "Question.2.1", "Question.2.2", 
"Question.3.1", "Question.3.2", "Question.4.1", "Question.4.2", 
"Question.5.1", "Question.5.2", "Question.6.1", "Question.6.2", 
"Question.7.1", "Question.7.2", "Question.8.1", "Question.8.2", 
"Question.9.1", "Question.9.2", "AQ.1", "AQ.2", "Task.1", "Task.2", 
"Task.3", "Task.4", "Task.5", "GQ.1", "GQ.2", "GQ.3", "GQ.4", 
"GQ.5", "GQ.6", "GQ.7", "GQ.8", "GQ.9", "GQ.10", "Feature.1", 
"Feature.2", "Feature.3", "Feature.4", "Feature.5", "Feature.6", 
"Feature.7"), class = "data.frame", row.names = c(NA, -15L))


I may have misunderstood the problem... but why don't you just to run cor on the data frame?

For instance:

data <- data.frame(q1=sample(1:5, 15, rep=1), 
          q2=sample(1:5, 15, rep=1), 
          q3=sample(1:5, 15, rep=1), 
          q4=sample(1:5, 15, rep=1), 
          q5=sample(1:5, 15, rep=1), 
          q6=sample(1:5, 15, rep=1), 
          q7=sample(1:5, 15, rep=1), 
          q8=sample(1:5, 15, rep=1), 
          q9=sample(1:5, 15, rep=1), 
          q10=sample(1:5, 15, rep=1))

print(cor(data))

You can even

image(cor(data), x=1:10, y=1:10, zlim=c(-1,1))

If you just need certain values of correlations just put the result of corr in a variable and pull out the results you need.

For instance, we want the correlation of column 2 with columns 5 to 10 we will:

corrs <- cor(data)
print(corrs[2, 5:10]) # or corrs[5:10, 2], the correlation matrix is symmetric


Subset the dataset explicitly and run the correlation command on that dataset. Assuming your variables are ordered properly, try something like this:

cor(dat[,c(2, 10:15)][,1]
cor(dat[,c(3, 10:15)][,1]

If they are not ordered, you'll just need to order them or name the variables in quotes instead. E.g.:

cor(dat[,c('v3', 'v10', 'v11', 'v12', 'v13', 'v14', 'v15')][,1]


Use subset command:

dtf <- subset(mtcars, select = c(mpg, hp, wt))
cor(dtf)
         mpg         hp         wt
mpg  1.0000000 -0.7761684 -0.8676594
hp  -0.7761684  1.0000000  0.6587479
wt  -0.8676594  0.6587479  1.0000000

Or use psych package and corr.test function:

library(psych)
corr.test(dtf)
Call:corr.test(x = dtf)
Correlation matrix 
      mpg    hp    wt
mpg  1.00 -0.78 -0.87
hp  -0.78  1.00  0.66
wt  -0.87  0.66  1.00
Sample Size 
    mpg hp wt
mpg  32 32 32
hp   32 32 32
wt   32 32 32
Probability value 
    mpg hp wt
mpg   0  0  0
hp    0  0  0
wt    0  0  0


The question seems motivated by the information overload that results from running corr on the entire dataframe. I have not used it much but the plyr package by Hadley Wickham of ggplot fame seems to offer some elegant solutions to subsetting and managing the output.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜