0

I create a vector called predictions, and the vector should be added new value in the sapply loop function.

However, when the loop finished, the predictions vector is still empty.
Then I tried predictions <- c(predictions, 1) in the command line for testing, and find 1 is added into predictions successfully.

It confused me, am I missing something to make it work?

    # 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)
    })
Jusleong
  • 571
  • 5
  • 10
  • 27
  • We cannot test your code as we do not have your object `test.coords`, `pred` and `models`. See this post http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example. My bet is that your object `one.pre` is `NULL` (that is to say "void"). – Pop May 19 '14 at 13:05
  • @Pop I can print the value of one.pre, so I am pretty sure that the value is not NULL – Jusleong May 19 '14 at 13:07
  • What is the class of `one.pred` ? Type `class(one.pred)` in R – Pop May 19 '14 at 13:09

2 Answers2

1

This should work:

 predictions <- c()
# loop
for (i in 1:rows.test.coords){
  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)
}

If you want to keep the sapplystructure, you shoul use it:

 predictions <- sapply(1:5, 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
  one.pre
})
Pop
  • 12,135
  • 5
  • 55
  • 68
1

Try the following:

predictions <- unlist(lapply(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
  # return value:
  apply(row.matrix,1,pred,models[[speciesName]])
})

The code will work as you expect whatever the length of the vector returned by apply is. This is because:

unlist(lapply(1:4, function(i) 1:i))
## [1] 1 1 2 1 2 3 1 2 3 4

Otherwise, you may use:

## ...
predictions <<- c(predictions, one.pre)
## ...

But such a solution has 2 drawbacks.

  1. you dynamically "extend" the size of the result vector (a time-consuming op, which in fact realocates the vector anew and copies its old contents)
  2. you're messing with variable scope (let's just say that's "inelegant")
gagolews
  • 12,836
  • 2
  • 50
  • 75
  • btw, what's the meaning of <<- – Jusleong May 19 '14 at 13:17
  • never see that before, sorry, i am new to R – Jusleong May 19 '14 at 13:17
  • @user3112938: See the [R intro](http://cran.r-project.org/doc/manuals/r-devel/R-intro.html#Assignment-within-functions) (and also [this section](http://cran.r-project.org/doc/manuals/r-devel/R-intro.html#Scope)) for more details. – gagolews May 19 '14 at 13:19