我正在使用 R 语言处理全局恐怖主义数据库
我正在尝试从名为“英国”的 gtd 中创建一个子集,其中包括大不列颠和北爱尔兰。
我可以用代码单独为他们做
uk <- subset(x=gtd, country_txt=="Northern Ireland")
我怎样才能使这个子集包含英国的数据?我明白为什么使用“&”不起作用,但我不知道形成正确子集的正确命令。
最佳答案
使用%in%
包含或否定由它形成的逻辑向量来进行排除:
uk <- subset(x=gtd, country_txt %in% c("Great Britain", "Northern Ireland") )
要排除该集合的成员:
not_uk <- subset(x=gtd, !country_txt %in% c("Great Britain", "Northern Ireland") )
也可以使用[
uk <- gtk[ gtd$country_txt %in% c("Great Britain", "Northern Ireland") , ]
关于r - 如何根据 R 中的 2 个标准创建子集?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22601011/