correlation function always NA

匿名 (未验证) 提交于 2019-12-03 02:33:02

问题:

I'm trying to find the correlations with this function but I just get NA for each input:

corr <- function(directory, threshold = 0) {    filenames <- list.files(directory, pattern="*.csv", full.names=TRUE)   csv <- lapply(filenames, read.csv)    correlations <- c()   c(correlations, lapply(csv, function(x){     cor(x$nitrate,x$sulfate)      print(cor(x$nitrate,x$sulfate))   }))    correlations   } 

If I change the function to print the nitrate and sulfate it does in fact have values, if I just pass in x to the cor function it complains about non-numeric values.

Why does it always print NA and not the actual correlation?

回答1:

The function cor has an argument use, which controls how it deals with missing observations, i.e. NAs. You might try setting it to use = "complete.obs".



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!