我有一个名为Country的变量,其中包含以下观察结果:
"AUS","AUT","BEL","BRA","BGR","CAN","CHN","CYP","CZE","DNK","EST","FIN","FRA","DEU", "GRC","HUN","IND","IDN","IRL","ITA","JPN","KOR","LVA", "LTU","LUX","MLT","MEX","NLD", "POL","PRT","ROU","RUS","SVK","SVN","ESP","SWE","TWN","TUR","GBR","USA"
我想要进行分组,以便删除Country == LUX|MLT
的所有观察结果。
我试过(相当凌乱):
mydata <- subset(mydata, Country == c("AUS","AUT","BEL","BRA","BGR","CAN","CHN","CYP","CZE","DNK","EST","FIN","FRA","DEU", "GRC","HUN","IND","IDN","IRL","ITA","JPN","KOR","LVA", "LTU","MEX","NLD", "POL","PRT","ROU","RUS","SVK","SVN","ESP","SWE","TWN","TUR","GBR","USA")))
但不幸的是我收到了错误消息:
Warning messages:
1: In is.na(e1) | is.na(e2) :
longer object length is not a multiple of shorter object length
2: In `==.default`(Country, c("AUS", "AUT", "BEL", "BRA", "BGR", "CAN",:
longer object length is not a multiple of shorter object length
答案 0 :(得分:3)
这是我的解决方案。我使用grep
函数。
data <- Country[-grep("LUX|MLT", Country)]
答案 1 :(得分:1)
尝试使用%in%
代替==
。 ==
假设双方的长度相等(或者一方可以回收)。
mydata <- subset(mydata, Country %in% c("AUS",...,"USA")))