mardi 3 novembre 2015

r nested for loop with if statement error message

here the input data and my expectations (y).

# input:
mydf <- data.frame(matrix(1:10, ncol = 2))
x=c(1.5, 2.8, 4.1)
y=c(0, 0, 0)
mydf
x
y

# code
for (i in 1:(length(x))
    for (j in 2:(dim(mydf)[1])
    {{
        if (x[i])>mydf[(j-1),1]) & (x[i])<mydf[j,1]) 
            {y[i]<-mydf[(j-1),2]} 
        else {y[i]<-9999999999}
    }}
y

expectation for y: 6,7,9. I do not understand the error.

Aucun commentaire:

Enregistrer un commentaire