2014-02-05 38 views
0

我有一個CSV文件 -如何在R中讀取文件的同時執行兩個動作?

Data1.csv

BusinessNeedParent,BusinessNeedChild,Identifier 
a1,b1,45 
a2,b2,60 
a3,b3,56 

Data2.csv

IndustryFoccusParent,IndustryFocusChild,Identifier 
x1,y1,75 
x2,y2,66 
x3,y3,78 

Data3.csv

AdvertiserName,BusinessNeedNumber,IndustryFocusNumber,State,City 
worker,45,75,Calif,Los angeles 
workplace,45,66,Calif,San Diego 
platoon,60,66,Connec,Bridgeport 
teracota,56,78,New York,Albany 
worker,45,66,Calif,Los Angeles 


AdvertiserName,BusinessNeedParent,BusinessNeedChild,IndustryFocusParent,IndustryFocusChild,State,City 
worker,a1,b1,x1,y1,Calif,Los angeles 
workplace,a1,b1,x2,y2,Calif,San Diego 
platoon,a2,b2,x2,y2,Connec,Bridgeport 
teracota,a3,b3,x3,y3,New York,Albany 
worker,a2,b2,x2,y2,Calif,Los Angeles 

我想匹配的標識符和從Data1.csv和Data2.csv獲取BusinessNeedNumber和IndustryFocusNumber以獲得上述o本安輸出。

到目前爲止,代碼是這樣的 -

record <- read.csv("Data3.csv",header=TRUE) 
businessneedinformation <- read.csv("Data1.csv",header=TRUE) 
industryFocusinformation <- read.csv("Data2.csv",header=TRUE) 

我使用「合併」在一次一個,以便得到輸出,我想一起做

m <- merge(Data1,Data3,by.x="Identifier",by.y="BusinessNeedNumber") 
n <- merge(Data2,Data3,by.x="Identifier",by.y="IndustryFocusNumber") 

當我在一個大型數據集上執行它時,它會生成不同長度的輸出。 我想同時執行'm'和'n'。如果不合並,還有其他可用選項?怎麼做 ?新的R,任何幫助表示讚賞。

+0

你只能做兩個數據幀與'merge'在基地R.使用'join_all'函數從'plyr'包試試,或者如果你知道SQL,嘗試'sqldf'包。我發現我可以真正向你展示一個例子。嘿,漫長的一天 – rawr

回答

0

你可以嘗試:

m <- merge(Data1,Data3,by.x="Identifier",by.y="BusinessNeedNumber", all.y=TRUE) 
#use m instead of Data3 
n <- merge(Data2,m,by.x="Identifier",by.y="IndustryFocusNumber", all.y=TRUE) 
相關問題