DS1
DS1

Reputation: 35

How to get the row numbers having "zero" values in a column by using "which" option?

I am very beginner in R. I am struggling to get the row numbers for the zero values of a specific column (V2). When I am using other non zero values e.g. y[which(y$V2 == 0.916670024),] is working well. However for zero values e.g. y[which(y$V2 == 0.000000000),] it does not show me the row numbers corresponding the zero values. Is there anyone know how to get rows number for a column with a Zero Values in R?

data: y

  1   0.083329998 0.166669995
  2   0.000000000 0.000000000
  3   0.166339993 0.082999997
  4   0.083329998 0.041669998
  5   0.000000000 0.125000000
  6   0.166089997 0.207849994
  7   0.208330005 0.041669998
  8   0.125000000 0.125000000
  ..........................

For Non zero values:

> y[which(y$V2 == 0.916670024),]

For Zero values:

> y[which(y$V2 == 0.000000000),]

For Non zero values:

      V1          V2
  145 0.083329998 0.916670024
  154 0.208330005 0.916670024
  157 0.333330005 0.916670024
  166 0.458330005 0.916670024
  169 0.583329976 0.916670024
  178 0.708329976 0.916670024
  181 0.833329976 0.916670024
  190 0.958329976 0.916670024

For Zero values:

[1] V1 V2
<0 rows> (or 0-length row.names)

Upvotes: 1

Views: 396

Answers (1)

akrun
akrun

Reputation: 887951

It may be because the values are not exactly equal to 0. This can be checked by taking the difference with 0. By

y$V2[2] - 0

In such cases, an option is to round and check

which(round(y$V2) == 0)

Upvotes: 1

Related Questions