Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filter by column index number data.table R

Tags:

r

data.table

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.

like image 615
Jantje Houten Avatar asked Nov 07 '25 18:11

Jantje Houten


2 Answers

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].

like image 101
Ronak Shah Avatar answered Nov 09 '25 07:11

Ronak Shah


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" ]
like image 37
zx8754 Avatar answered Nov 09 '25 06:11

zx8754



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!