0

How do i get the number of rows that matches my condition: value of column 1 is lower than value of column2 plus 1?

nrow(subset(dataset, column1 <= column2 + 1))

When i run this command i get the following warning:

Warning message: In Ops.factor(column2, 1) : ‘+’ not meaningful for factors

And when i Run:

nrow(subset(dataset, column1 <= column2))

I get:

Warning message: In Ops.factor(vg, (column2)) : ‘<=’ not meaningful for factors

  • 2
    `sum(dataset$column1 <= (dataset$column2 + 1))` should give you the number of value for which the condition is `TRUE` – cderv Nov 02 '16 at 16:29
  • ... and dataset[dataset$column1 <= (dataset$column2 + 1), ] will give you the rows. – m-dz Nov 02 '16 at 16:29
  • 2
    Please also read this thread and try to give more details in your future questions: http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – m-dz Nov 02 '16 at 16:31
  • sum(dataset$column1 <= (dataset$column2 + 1)) returns me this: [1] NA Warning message: In Ops.factor(dataset$column2, 1) : ‘+’ not meaningful for factors – Joel Pinto Nov 02 '16 at 16:49

1 Answers1

0

Could you please share the data you are using?

If the column 2 has numeric values, try changing the column's class from factor to numeric. Same for the column 1.

Maybe something like this could work:

sum(as.numeric(as.character(dataset$column1)) <= (as.numeric(as.character(dataset$column2)) + 1))
ira
  • 2,542
  • 2
  • 22
  • 36