4

Interesting I am unable to find a way to filter using the column number. I do not know the name of the column because it changes name, but I always know the position of the column.

This seems pretty trivial but it seems like I can only reference the i portion using the column name.

table = data.table(one = c(1,2,3), two = c("a","b","c"))

> table
   one two
1:   1   a
2:   2   b
3:   3   c

I do not know that the second column is "two". I just want to filter by second column.

> table[two == "a"]
   one two
1:   1   a

UPDATE:

As Ronak described, I could use

> table[table[[2]]=="a"]

   one two
1:   1   a

However I would next like to update this same column, for example I would like to turn "a" into "c".

what I need:

> table
   one two
1:   1   c
2:   2   b
3:   3   c

I have tried:

> table[table[[2]]=="a", table[[2]]:= "c"]
> table
   one two    a    b    c
1:   1   a    c    c    c
2:   2   b <NA> <NA> <NA>
3:   3   c <NA> <NA> <NA>

So it seems like I am taking all the values in the second column and creating new columns for them instead of just changing the filtered rows to c.

> table[table[[2]]=="a", table[2]:= "c"]

Error in `[.data.table`(table, table[[2]] == "a", `:=`(table[2], "c")) : 
  LHS of := must be a symbol, or an atomic vector (column names or positions).

So I think I need to know the position of the second column.

5 Answers5

5

Using [[ works :

library(data.table)

dt <- data.table(a = 1:5, b = 2:6)
dt[dt[[1]] == 1]

#   a b
#1: 1 2

This gives the same output as dt[a == 1].

Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
2

As we know we need the 2nd column, get the 2nd column name, and use "variable as column name", see example:

library(data.table)
d <- data.table(one = c(1,2,3), two = c("a","b","c"))

# get the 2nd column name
myCol <- colnames(d)[ 2 ]

# subset
d[ get(myCol) == "a", ]

# subset and update
d[ get(myCol) == "a", (myCol) := "c" ]
zx8754
  • 52,746
  • 12
  • 114
  • 209
2

We can use .SD

dt[dt[, .SD[[1]] == 1]]
#   a b
#1: 1 2

data

dt <- data.table(a = 1:5, b = 2:6)
akrun
  • 874,273
  • 37
  • 540
  • 662
1

You can also try this:

table[[2]][table[[2]]=="a"] <- "c"
table
> table
   one two
1:   1   c
2:   2   b
3:   3   c
lovestacksflow
  • 521
  • 3
  • 14
0

I have figured it out:

> table[table[[2]]=="a", colnames(table)[2]:= "c"]
> table
   one two
1:   1   c
2:   2   b
3:   3   c

Thanks!