Search code examples
rselectdata.tabledata-manipulationsql-like

Using the %LIKE% Statement for a Condition Stored in a Data Frame


I have these data frames:

 my_data = data.frame(col1 = c("abc", "bcd", "bfg") , id = 1:3)
 my_data_1 = data.frame(col1 = c("abc", "byd", "bgg") , id = 1:3)

I want to select all rows from "my_data_1" which are equal to the first row and first column of "my_data". In other words, select all rows from "my_data_1" where the row contains values "abc". I tried to use the following code:

library(data.table)

 my_data[like(my_data_1, my_data[1,1])]

In my opinion, only the first row should have been outputted. However, all rows seem to have been outputted:

  col1
1  abc
2  bcd
3  bfg

Can someone please show me what I am doing wrong?

Thank you!


Solution

  • I always find it useful to use the data.table differentiation, that if you select the column name directly the result is a vector. However, if you are trying to use data.table you should create the data.frames like that.

    my_data = data.table(col1 = c("abc", "bcd", "bfg") , id = 1:3)
    my_data_1 = data.table(col1 = c("abc", "byd", "bgg") , id = 1:3)
    
    
    my_data[like(col1, my_data[1,col1])]