-2

How can I incorporate criteria into a table(x,y) function? Instead of subsetting the main data frame and then running table() on each subset, can I save a step or two and just write table() with some sort "if" functionality written into it?

  • 1
    It would be easier to help you if you provided a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input data and desired output. The `table()` function doesn't do subsetting, But subsetting the data frame shouldn't be that hard. – MrFlick Mar 09 '17 at 21:12

1 Answers1

0

An example of the data set would be great. Hard to imagine what you're after at the moment.

But yes, you can write an if/else statement:

if(grepl("^1[.]0{3}, dataset)==TRUE)) {
    print("1.000")
}    else    {
    print("not 1.000")
}

Here, I'm asking it to find the pattern 1,000 in the dataset named dataset. If not, it'll return the else statement.

You can see other examples around too. Maybe look at the function summary of if, and how to use regex (regular expressions).

Hope that helps.

dthorbur
  • 155
  • 1
  • 11