>im 尝试在 r 中使用相关矩阵 我的数据有一堆空值或不适用值 我目前的方法是将这些空值转换为 0 这有效,但它会导致矩阵不准确,因为某些具有相关性的列被 0 您知道修复NA值的任何解决方案吗?
这是我的代码:
mydata = read.csv("exoplanet.csv")
res2 <- cor(mydata[sapply(mydata, function(x) is.numeric(x))])
res2[is.na(res2)] <- 0
corrplot(res2, type = "upper", order = "hclust",
tl.col = "black", tl.srt = 45)
这是我尝试过的解决方法:
mydata = read.csv("exoplanet.csv")
mydata = lapply(mydata, as.numeric)
mydata = as.matrix(as.numeric(unlist(mydata)))
//the reason i do this is because otherwise i get a list to double error
现在,当我尝试使用此图时,出现此错误:
The matrix is not in [-1, 1]! or In as.dist.default(1 - corr) : non-square matrix
您需要删除只有 1 种值类型的列(不包括 NA(:
x = read.csv("./Downloads/Exoplanet extract new.csv")
is_num = sapply(x,is.numeric)
not_mono = sapply(x,function(i)length(unique(i[!is.na(i)])))>1
然后:
cor(x[,is_num & not_mono],use="p")
由于某些列之间缺少一些值,您仍然有 NA 单元格,但很可能这是您能做的最好的事情。