Paste logical conditions in R

I have now rewriten my problem to make it clearer

I want to replace a condition like this where var is a variable in dataframe (dataframe$var) with a paste or other solution as I do have so many condition values(?) (a, b and c in my example).

subdataframe<-dataframe[var=="a"|var=="b"|var=="c",]

I have tried to make a list(?) of the condtion values.

sample<-c("a","b","c")

And to then use paste to make the logical condition

subdataframe<-dataframe[paste("var",sample,sep="==",collapse="|"),]

But that doesn't work

Help please =)

Marcus

Answers


Heed fortune(106):

> fortune(106)

If the answer is parse() you should usually rethink the question.
   -- Thomas Lumley
      R-help (February 2005)

So I would encourage you to rethink what you're trying to do...

I would guess that you could use match or %in% to achieve your desired result, but you haven't told us what you're trying to do.

> sample <- c("a","b","c")
> var <- c("a","d","c")
> eval(parse(text=paste("var==",sample,"",sep="'",collapse="|")))
[1]  TRUE FALSE  TRUE
> var %in% sample
[1]  TRUE FALSE  TRUE

Need Your Help

Mouse Events and FPS

javascript canvas html5-canvas

I'm trying to record user's mouse movements by using canvas and javascript. Normally, each point is saved when there is a new mouse event from the browser, but obviously yhis causes problems when

Linq2Sql Contains

c# linq linq-to-sql

I have two lists of Data Holders.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.