Getting error using cor() in For loop in R for list of csv files -
i have list of csv files i'm iterating through
corr <- function(directory) { filename <- list.files(directory) x <- paste(directory, "/", sep="") monitor_list <- paste(x, filename, sep="") completelocations <- null corrdata <- null
i'm trying use cor() generate data frame has cor values 2 columns in each csv file.
(monitor in monitor_list) { locationinfo <- read.csv(monitor) corrdata <- rbind(corrdata, cor(locationinfo$sulfate, locationinfo$nitrate, use = "complete.obs")) completelocations <- rbind(completelocations, sum(complete.cases(locationinfo))) }
and throws error
error in cor(locationinfo$sulfate, locationinfo$nitrate, use = "complete.obs") : no complete element pairs
i know can done because when run exact same thing outside of loop, return correlation last csv file in list.
print(cor(locationinfo$sulfate, locationinfo$nitrate, use = "complete.obs"))
i'm relatively new r , think i'm missing inside of loop, i'm not sure is...
here 1 way replicate error
dd<-data.frame(x=c(1:10, rep(na,10)), y=c(rep(na, 10), 1:10)) cor(dd$x, dd$y, use="complete.obs")
so seems either of nitrate , sulfate values 'na' or when 1 not 'na', other 'na'. check locationinfo
data.frame looks way expect.
Comments
Post a Comment