I get a nans produced warning message.How can I solve problem?

aDemand = as.data.frame(read.table("locations.dat", header = FALSE))
rho.data=as.data.frame(read.table("rhoset.dat", header = FALSE))
dij= as.data.frame(read.table("distances.dat", header = FALSE))

xi = as.vector(aDemand[,1])
xj = as.vector(aDemand[,1])
yi = as.vector(aDemand[,2])
yj = as.vector(aDemand[,2])
ri = as.vector(rho.data[,2])
rj = as.vector(rho.data[,2])

for(i in 1:length(xi)){
 for (j in 2:length(xi)){


Break your formulas into pieces to see where the NaN comes from. Quite likely it's from the sqrt().

It looks like there are some * missing in between coefficients and variables and in between parentheses.

This code could also benefit from vectorizing.

use na.omit() to eliminate those null records

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.