나는 predictions라는 벡터를 만들고, 벡터는 sapply 루프 함수에 새로운 값을 추가해야합니다.Spply 함수 요소의 R을 벡터에 더할 수 없습니다
그러나 루프가 끝나면 예측 벡터는 여전히 비어 있습니다.
그런 다음 테스트를 위해 predictions <- c(predictions, 1)
을 명령 줄에서 시도했는데 1이 예측에 성공적으로 추가되었습니다.
나를 혼란스럽게 만들었습니다. 제대로 작동하지 못했습니다.
# create an empty vector
predictions <- c()
# loop
sapply(1:rows.test.coords, function(i){
each.test.row <- test.coords[i,]
speciesName <- each.test.row[3]
location <- c(each.test.row[1], each.test.row[2])
row.matrix <- matrix(as.matrix(as.numeric(location)),ncol=2)
# Get numeric value one.pre and going to add into predictions vector
one.pre <- apply(row.matrix,1,pred,models[[speciesName]])
# Add element into vector
predictions <- c(predictions, one.pre)
})
우리는 당신의 객체가'test.coords','pred' 및'models'이 아니므로 여러분의 코드를 테스트 할 수 없습니다. 이 게시물을 참조하십시오 http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example. 제 생각에 당신의 객체'one.pre'은'NULL'입니다 (즉, "void"). – Pop
@Pop one.pre의 값을 인쇄 할 수 있으므로 값이 NULL이 아닌 것이 확실합니다. – Jusleong
'one.pred'의 클래스는 무엇입니까? R – Pop