Paste0, subset Error: 'subset' must be logical

1.3k views Asked by At

I would like to use paste0 to create a long string containing the conditions for the subset function.

I tried the following:

#rm(list=ls())
set.seed(1)
id<-1:20
ids<-sample(id, 3)
 
 d <- subset(id, noquote(paste0("id==",ids,collapse="|")))

I get the

Error in subset.default(id, noquote(paste0("id==", ids, collapse = "|"))) : 
  'subset' must be logical

I tried the same without noquote. Interestinly when I run

noquote(paste0("id==",ids,collapse="|"))

I get [1] id==4|id==7|id==1. When I then paste this by hand in the subset formula

d2<-subset(id,id==4|id==7|id==1)

Everything runs nice. But why does subset(id, noquote(paste0("id==",ids,collapse="|"))) not work although it seems to be the same? Thanks a lot for your help!

0

There are 0 answers