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.