R – using string as input in a function

I have a table with sample points from which I need to take subsets based on occurrence of these sample points in certain vegetation types. I have to repeat this many times for different combinations of vegetation types, so I have written a small script that gives me the conditions for sub-setting my table as a string, e.g.,

"VT==1 | VT==3"

But how to convert this string to be used in the subset function?

subset(data,VT==1 | VT==3)

Thanks to the invaluable R help archive I found that the following should work:

condition1 <- "VT==1 | VT==3"
subset.table <- subset(PNVs, eval(parse(text=conditions1)))

This shows again one reason for the success of R, the incredible amount of information and help available online. And if you can’t find it, there is a good change that somebody can help you out on the R help mailing list.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s