Judá García
Judá García

Reputation: 33

Create subset with %like% operator

I'm looking for some help to create subsets using %like% operator in R.

I have a table called 'pruebas1', which contains this information:

      scenario_name | land_consumption | land_consumption_pct
Contención al 30%      692.00              11.081468525813
Contención al 50%      221.23               3.542703786613
Contención al 70%       94.98               1.520975451494
Contención al 95%       69.29               1.109583760966

And more rows. They share a pattern, the percentage value '30%', '50%'

I want to create a subset for each percentage value, and I tried to do it with this code:

for (i in 1:33){
  if (prueba1$scenario_name %like% '%30%'){
    esc_30[[i]]<-prueba1$scenario_name[[i]]
  }
} 

The result is an object with no data. I built this with a friend and we are new to this. As you can see we need help first to use correctly the %like% operator and of course make a loop to create a subset for the different percentages values.

You can help us with specific links or help with the code directly.

Upvotes: 2

Views: 2969

Answers (2)

Vladimir Volokhonsky
Vladimir Volokhonsky

Reputation: 155

If you want to avoid using regexp you should use "fixed" argument in the grepl. The %like% in data.table is a wrapper for grepl.

So, you could try something like:

esc30<-prueba1$scenario_name[grepl("30%",prueba1$scenario_name,fixed=T)]

If you want to get all the columns:

esc30<-prueba1[grepl("30%",prueba1$scenario_name,fixed=T),]

However, if you want to not subset items containing "30%" in the middle of the text, you should learn regexps.

Upvotes: 0

Hong Ooi
Hong Ooi

Reputation: 57686

You're probably thinking of the SQL LIKE operator, where x LIKE '%foo%' means any values that contain 'foo' in any position.

The equivalent for the data.table %like% would be x %like% ".*foo.*". This is because %like% works with regular expressions. In a regular expression, the string .* means "any character repeated 0, 1 or multiple times".

In R, see ?regex for how R handles regular expressions.

Upvotes: 2

Related Questions