How to select R data.table rows based on substring match (a la SQL like)
问题 I have a data.table with a character column, and want to select only those rows that contain a substring in it. Equivalent to SQL WHERE x LIKE '%substring%' E.g. > Months = data.table(Name = month.name, Number = 1:12) > Months["mb" %in% Name] Empty data.table (0 rows) of 2 cols: Name,Number How would I select only the rows where Name contains "mb"? 回答1: data.table has a like function. Months[like(Name,"mb")] Name Number 1: September 9 2: November 11 3: December 12 Or, %like% looks nicer : >