r cran:如何使用&生成实际值而不是向量中的观察数量。功能?

时间:2018-03-07 13:25:27

标签: r

我的csv中的数据如下所示: 变量1,第1列:使馆名称

Embassy_Name       
Argentina           
Argentina           
Belgium             
Bulgaria            
Switzerland         
Switzerland         
Switzerland    

变量2,第2列:城市名称

City_name
Chicago    
Boston    
LA  
LA   
Boston   
LA      
Chicago    

现在我需要查找哪些国家/地区在瑞士以上的所有3个城市都有大使馆,所以我编写了代码:

city2<-length(Embassy[Embassy$City== "Los Angeles" & Embassy$City== "Chicago" & Embassy$City== "Boston" ,])

所有它返回的是有5个国家在所有3个国家都有大使馆....但我需要知道哪些国家需要R给我一个文本字符串列表,准确说明哪个国家是哪个? / p>

渴望的输出例如:他们在所有3个城市拥有大使馆的国家名单:  瑞士,印度,法国,挪威,日本

实际数据是156 obs:

structure(list(embassy = structure(c(" Afghanistan ", " Argentina ", 
" Argentina    ", " Australia ", " Australia ", " Austria ", 
" Austria ", " Azerbaijan ", " Bangladesh ", " Barbados ", " Belgium ", 
" Belgium ", " Belize ", " Belize ", " Bolivia ", " Bosnia and Herzegovina ", 
" Brazil ", " Brazil ", " Bulgaria ", " Bulgaria ", " Canada ", 
" Canada ", " Cape Verde    ", " Chile ", " Chile ", " China ", 
" China ", " Colombia ", " Colombia ", " Costa Rica ", " Croatia ", 
" Croatia ", " Cyprus ", " Czech Republic ", " Czech Republic ", 
" Denmark ", " Dominican Republic      ", " Dominican Republic ", 
" Dominican Republic ", " Ecuador ", " Egypt ", " Egypt ", " El Salvador ", 
" El Salvador ", " El Salvador      ", " Estonia    ", " Ethiopia ", 
" Finland ", " Finland ", " France ", " France ", " France ", 
" Germany ", " Germany      ", " Germany ", " Greece ", " Greece ", 
" Greece ", " Grenada ", " Guatemala      ", " Guatemala ", " Haiti ", 
" Haiti ", " Honduras ", " Honduras ", " Hungary ", " Hungary ", 
" Iceland ", " India    ", " Indonesia ", " Indonesia ", " Iraq      ", 
" Ireland ", " Ireland ", " Israel ", " Israel ", " Israel ", 
" Italy ", " Italy ", " Italy ", " Jamaica ", " Japan      ", 
" Japan ", " Japan      ", " Jordan ", " Kenya ", " Kuwait      ", 
" Latvia ", " Lebanon ", " Liberia ", " Liechtenstein    ", " Lithuania ", 
" Luxembourg      ", " Macedonia ", " Malaysia ", " Malta ", 
" Mauritius ", " Mexico ", " Mexico ", " Mexico ", " Monaco ", 
" Mongolia ", " Morocco ", " Nepal ", " Netherlands ", " New Zealand    ", 
" Nicaragua ", " Norway ", " Pakistan ", " Pakistan ", " Paraguay ", 
" Peru ", " Peru ", " Peru ", " Philippines ", " Philippines ", 
" Poland ", " Poland ", " Portugal ", " Portugal ", " Republic of Korea ", 
" Republic of Korea ", " Romania ", " Romania ", " Rwanda ", 
" Saint Kitts and Nevis    ", " Sao Tome and Principe ", " Saudi Arabia ", 
" Serbia ", " Singapore ", " Slovakia ", " Slovakia ", " South Africa ", 
" South Africa ", " Spain ", " Spain ", " Spain ", " Sri Lanka      ", 
" Sri Lanka ", " Sweden ", " Switzerland ", " Tanzania ", " Thailand    ", 
" Thailand ", " Turkey ", " Turkey ", " Turkey ", " United Arab Emirates ", 
" Uganda ", " Ukraine    ", " United Arab Emirates ", " United Kingdom ", 
" United Kingdom ", " Uruguay ", " Venezuela ", " Venezuela ", 
City = c("Los Angeles", "Chicago", "Los Angeles", "Chicago", 
"Los Angeles", "Los Angeles", "Chicago", "Los Angeles", "Los Angeles", 
"Chicago", "Chicago", "Los Angeles", "Los Angeles", "Chicago", 
"Los Angeles", "Chicago", "Chicago", "Boston", "Los Angeles", 
"Chicago", "Los Angeles", "Boston", "Boston", "Chicago", "Los Angeles", 
"Chicago", "Los Angeles", "Chicago", "Boston", "Los Angeles", 
"Los Angeles", "Chicago", "Chicago", "Los Angeles", "Chicago", 
"Chicago", "Chicago", "Los Angeles", "Boston", "Chicago", "Los Angeles", 
"Chicago", "Chicago", "Los Angeles", "Boston", "Chicago", "Los Angeles", 
"Los Angeles", "Chicago", "Los Angeles", "Chicago", "Boston", 
"Los Angeles", "Chicago", "Boston", "Los Angeles", "Chicago", 
"Boston", "Chicago", "Chicago", "Los Angeles", "Chicago", "Boston", 
"Los Angeles", "Chicago", "Los Angeles", "Chicago", "Chicago", 
"Chicago", "Chicago", "Los Angeles", "Los Angeles", "Chicago", 
"Boston", "Chicago", "Los Angeles", "Boston", "Los Angeles", 
"Chicago", "Boston", "Chicago", "Los Angeles", "Chicago", "Boston", 
"Chicago", "Los Angeles", "Los Angeles", "Chicago", "Los Angeles", 
"Chicago", "Chicago", "Chicago", "Chicago", "Chicago", "Los Angeles", 
"Chicago", "Chicago", "Chicago", "Los Angeles", "Boston", "Chicago", 
"Chicago", "Chicago", "Chicago", "Chicago", "Chicago", "Los Angeles", 
"Chicago", "Los Angeles", "Chicago", "Los Angeles", "Chicago", 
"Los Angeles", "Boston", "Chicago", "Los Angeles", "Los Angeles", 
"Chicago", "Chicago", "Boston", "Los Angeles", "Chicago", "Los Angeles", 
"Chicago", "Chicago", "Los Angeles", "Chicago", "Los Angeles", 
"Chicago", "Chicago", "Los Angeles", "Chicago", "Chicago", "Los Angeles", 
"Chicago", "Los Angeles", "Boston", "Los Angeles", "Chicago", 
"Chicago", "Los Angeles", "Crystal Lake", "Los Angeles", "Chicago", 
"Chicago", "Los Angeles", "Boston", "Los Angeles", "Chicago", 
"Chicago", "Boston", "Los Angeles", "Chicago", "Chicago", "Chicago", 
"Boston"), 

1 个答案:

答案 0 :(得分:1)

#install.packages("sqldf")
library(sqldf)

Embassy_Name <- c("Argentina",           
              "Argentina",           
              "Belgium",             
              "Bulgaria",           
              "Switzerland",         
              "Switzerland",         
              "Switzerland"  )

City_name <- c("Chicago",    
           "Boston",    
           "LA",  
           "LA",   
           "Boston",   
           "LA",      
           "Chicago")

df <- as.data.frame(cbind(Embassy_Name,City_name))

df_2 <- sqldf('SELECT Embassy_Name,
          COUNT(City_name) AS COUNT
  FROM df
  GROUP BY Embassy_Name
  HAVING COUNT > 2'
  )

list <- as.character(unique(df_2$Embassy_Name))

print(list)