user1917231
user1917231

Reputation: 61

Multiple string matching possibilities condition in R

I have a variable called Country and I would like to create a subset where Country equals Australia and Greece. In this case, I tried to do like this and failed.

CUTdata <- subset(NEWdata, Country == c('Australia','Greece'))

This does not work. What is another way to achieve this kind of logic?

Upvotes: 0

Views: 333

Answers (3)

akrun
akrun

Reputation: 887118

Here is an option using data.table. We convert the 'data.frame' to 'data.table', set the key column as 'Country', and subset the row in the dataset where the 'Country' is 'Australia' or 'Greece'.

setDT(NEWdata, key='Country')[c('Australia', 'Greece')]
#     Country       value
#1: Australia  0.59826911
#2: Australia -0.08554095
#3: Australia  0.11953107
#4: Australia -0.94382724
#5:    Greece -1.31690812
#6:    Greece -0.33498679
#7:    Greece  0.51633570
#8:    Greece -0.60515695
#9:    Greece -0.64859151

data

set.seed(24)
NEWdata <- data.frame(Country = sample(c("Australia", 
  "Greece", "USA", "UK"), 20, replace=TRUE), value=rnorm(20))

Upvotes: 0

CuriousBeing
CuriousBeing

Reputation: 1632

.... Or use the sqldf package :)

library(sqldf)
sqldf("select * from Newdata where Country in ('Australia' ,'Greece')")

Upvotes: 0

myloginid
myloginid

Reputation: 1473

Use grep. Just like in Unix. You can also ignore case and do partial matches.

CUTdata <- grep( 'Australia|Greece' , NEWdata, value = T, ignore.case = T)

Upvotes: 2

Related Questions