Yang Yang
Yang Yang

Reputation: 900

How to write a formula in a loop for autoKrige {automap} in R

I have a large matrix and the column names are as follows:

colid=vector(length = 60)
for(i in 1"60) {
  colid[i]=paste0("V",i)
}

When I use the autoKrige function in automap, a formula must be written in such format: V1~1. When I try to do this using a loop, an error occurs:

library(automap)
value=list()
for(i in 1:60) {
  value[[i]]=autoKrige(colid[i]~1,Mydata,new_data = newgrid)
}

Error: too many spatial dimensions: 3068 In addition: Warning message:
NAs introduced by coercion 

Then I try to fix this, I test the formula:

> colid[10]~1
colid[10] ~ 1

So, the problem is because the formula is not in the correct format. I wonder how I can fix this? Thanks a lot.

Upvotes: 0

Views: 284

Answers (1)

Sébastien Rochette
Sébastien Rochette

Reputation: 6661

Use function as.formula to transform characters as formula:

colid=vector(length = 60)
value=list()
for(i in 1:60) {
  colid[i] <- paste0("V",i, " ~ 1")
  value[[i]]=autoKrige(as.formula(colid[i]),Mydata,new_data = newgrid)
}

Upvotes: 2

Related Questions