GCGM
GCGM

Reputation: 1083

Error creating variable in R

I am pasting several words together but I need the output without the quotation and index. For that I am using the following code:

condition_2<-cat(noquote(paste("S1_images", "[[1]]",sep="")))

When I run it, I get the right result on the terminal: S1_images[[1]]. But, if I check check the variable condition_2 to be sure that the text has been saved, I get the following output

NULL

This is creating a problem since I need that variable to be used as reference later on the code.

-- EDIT --

I need the index [1] out since I want to use condition_2 as a reference in another line. If I do not remove it, this is what happens:

training_r<-rasterize(training,condition_2, field=test$_ID)

Which output is:

Error in (function (classes, fdef, mtable)  : 
  unable to find an inherited method for function ?rasterize? for signature ?"SpatialPolygonsDataFrame", "noquote"?

If I remove it (using cat), this is what happens:

> training_r<-rasterize(training,condition_2, field=test$ID)

Error in (function (classes, fdef, mtable)  : 
  unable to find an inherited method for function ?rasterize? for signature ?"SpatialPolygonsDataFrame", "NULL"?

Any idea?

Upvotes: 1

Views: 164

Answers (1)

Prem
Prem

Reputation: 11985

You can create a variable using

condition_2 <- parse(text = paste0("S1_images", "[[1]]"))


and then while using this variable you can wrap it with eval e.g.

rasterize(training, eval(condition_2), field = test$_ID)

Upvotes: 2

Related Questions