Reputation: 1359
I currently have the dataset newdat, a dataframe containing item scores:
set.seed(1)
newdat <- setNames(data.frame(replicate(5,sample(1:5,10,replace=TRUE))),paste0("i",1:5))
i1 i2 i3 i4 i5
1 2 2 5 3 5
2 2 1 2 3 4
3 3 4 4 3 4
4 5 2 1 1 3
5 2 4 2 5 3
6 5 3 2 4 4
7 5 4 1 4 1
8 4 5 2 1 3
9 4 2 5 4 4
10 1 4 2 3 4
I also have the character strings "newCV" and "newDV" which are:
newCV <- c("i3","i2")
newDV <- c("i1")
I am attempting to correlate DV with all of the items except itself, and the items contained in newCV. I have tried the following:
corr<-cor(newdat,use="complete.obs")[-which(colnames(newdat)==c(newCV,newDV)),which(colnames(newdat)==c(newCV,newDV))]
Which works if there is nothing found in CV, but if there is something in CV I get an error and no results. Any thoughts? Thank you!
Upvotes: 2
Views: 341
Reputation: 25874
If you only want to calculate the specific correlations you can select what columns to pass to cor
cor(newdat[newDV], newdat[!(names(newdat) %in% c(newCV, newDV))],
use="complete.obs")
Upvotes: 2