Question

I am working with the global terrorism database in R

I am trying to make a subset from the gtd called United Kingdom to include Great Britian and Northern Ireland.

I can do it for them individually with the code

 uk <- subset(x=gtd, country_txt=="Northern Ireland")

How can I also make this subset include data for Great Britian? I understand why using "&" doesn't work, but I don't know the right command to form the correct subset.

Was it helpful?

Solution

Use %in% for inclusion or negation of a logical vector formed with it to do exclusion:

uk <- subset(x=gtd, country_txt %in% c("Great Britain", "Northern Ireland") )

To exclude members of that set:

not_uk <- subset(x=gtd, !country_txt %in% c("Great Britain", "Northern Ireland") )

Can also do that with [

uk <- gtk[ gtd$country_txt %in% c("Great Britain", "Northern Ireland") , ]

OTHER TIPS

Since you want to include both countries, you need to use a condition that is valid when the country text is either of the two - this implies an OR syntax. You could try

uk<- subset(x=gtd, (country_txt=="Northern Ireland") | (country_txt=="Great Britain") )

See How to combine multiple conditions to subset a data-frame using "OR"? for more discussion on this.

use & (not &&) or %in%

 country_txt=="Northern Ireland" | country_txt == "GB"

OR

 country_txt %in%  c("GB", "Northern Ireland")

if using %in%, make sure that the right-hand value is a vector (ie, use c())

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top