correlation - Case wise delete of values inside cor() in R -
consider following survey data:
data <- replicate(10 ,sample(c(1,2,3,4), 1000, replace = true)) %>% as.data.frame() v1:v9 variables 1 = "good", 2 = "okey" , 3 = "not good" , 4 = "don't know" while v10 ordinal variable 1 = "good", 2 = "not good", 3 = "don't know" , 4 = "don't want answer".
i interested in calculating simple correlation matrix using cor() on these variables. however, want calculate between values mean something. is, 1,2,3 v1:v9 , 1,2 v10.
in other words, want case wise delete of value > 3 v1:v9 , same values > 2 v10 within cor() function.
this similar use argument?
the way have managed solve mutating these values na.
library("dplyr") data_test <- data_test %>% mutate_each(funs(ifelse(. > 3, na, .)), -v10) %>% mutate(ifelse(v10 > 2, na, v10)) cor(data_test, use = "complete.obs") but there better way not rely on modifying data.
ps. there are, of course, more adequate ways of calculating correlation between ordinal variables.
the answer question more simple thought.
as @zx8754 points out should careful when choosing correlation method categorical variables.
anyways, change use = "pairwise.complete.obs" in cor()
however, still need mutate 4 na.
Comments
Post a Comment