dissertationstacker
dissertationstacker

Reputation: 21

Finding multiple variables within a single column on data.table

Sorry if this isn't laid out correctly, still in very early stages of R and these forums!

I have an extremely large, global dataset split into countries using the ISO country code however I am only after the European countries.

I can successfully highlight one variable from the dataset

test1[country_code=="BE",]

How can I extract all the European codes? I have tried & and | but unsuccessfully.

Upvotes: 1

Views: 92

Answers (2)

Harshit_Mishra
Harshit_Mishra

Reputation: 86

This creates a new data table (single column) with the list of all unique country codes :

country.codes <- as.data.table(unique(test1[,country_code]))

You can easily determine the European country codes from the newly extracted small list of country codes.

Upvotes: 1

Liz Young
Liz Young

Reputation: 408

try defining a list of Euro countries, then make use of %in%

euro_codes <- c('BE', 'DE', ...)
test1[ country_code %in% euro_codes, ]

Upvotes: 5

Related Questions