1

I'm trying to subset a data.table based on a condition but I want also to get its original row number/s from the data.table. What I read here is that you just need to set the which to TRUE but it only returns the actual row number not with its entire row.

Boston[medv == min(medv), which=TRUE] this one returns [1] 399 406 which means that those row met the condition.

If which is not set to TRUE, i.e. Boston[medv == min(medv)] it returns

      crim zn indus chas   nox    rm age    dis rad tax ptratio  black lstat medv
1: 38.3518  0  18.1    0 0.693 5.453 100 1.4896  24 666    20.2 396.90 30.59    5
2: 67.9208  0  18.1    0 0.693 5.683 100 1.4254  24 666    20.2 384.97 22.98    5

which it returns the row number differently (i.e. 1 & 2).

What I'm trying to achieve is to look like this,

      crim zn indus chas   nox    rm age    dis rad tax ptratio  black lstat medv
399: 38.3518  0  18.1    0 0.693 5.453 100 1.4896  24 666    20.2 396.90 30.59    5
406: 67.9208  0  18.1    0 0.693 5.683 100 1.4254  24 666    20.2 384.97 22.98    5

where the row number is 309 and 406.

isemaj
  • 557
  • 1
  • 6
  • 19

1 Answers1

1

data.table doesn't use row names so I don't think what you are trying to achieve is possible. Instead you could just create a new column holiding the original row id.

irisDT <- data.table(iris)
irisDT[, rowid := .I][Sepal.Width == 3.5]


   Sepal.Length Sepal.Width Petal.Length Petal.Width Species rowid
1:          5.1         3.5          1.4         0.2  setosa     1
2:          5.1         3.5          1.4         0.3  setosa    18
3:          5.2         3.5          1.5         0.2  setosa    28
4:          5.5         3.5          1.3         0.2  setosa    37
5:          5.0         3.5          1.3         0.3  setosa    41
6:          5.0         3.5          1.6         0.6  setosa    44
s_baldur
  • 29,441
  • 4
  • 36
  • 69