2014-01-24 81 views
-2

我與R et Rstudio一起工作,我遇到了問題。如何在R中分配2個向量中的字符串

首先,我有2個向量:

cap-color    
cap-color    
odor      
odor      
odor      
odor      
gill-spacing    
gill-size    
gill-color    
stalk-surface-above-ring 
stalk-color-above-ring 
spore-print-color  
spore-print-color  
population    
population 

AND:

buff  
pink  
creosote 
foul  
musty  
pungent 
close  
narrow 
buff  
silky  
cinnamon 

chocolate 

green  

scattered 

several 

現在,我想的輸出:

cap-color ∈ {buff, pink} 
odor ∈ {creosote, foul, musty, pungent} 
gill-spacing = {close} 
gill-size = {narrow} 
gill-color = {buff} 
stalk-surface-above-ring = {silky} 
stalk-color-above-ring = {cinnamon} 
spore-print-color ∈ {chocolate, green} 
population ∈ {scattered, several} 
+0

'帽色< - C(「淺黃色」,「粉紅色」)' – Andrie

回答

1

使之成爲data.table然後粘貼矢量2通過矢量分組的元素1

library(data.table) 
dt <- data.table("Name"=vector1,"Var"=vector2) 
res <- dt[,paste(Var,collapse=","),by=Name] 

res 
        Name       V1 
1:    cap-color     buff,pink 
2:      odor creosote,foul,musty,pungent 
3:    gill-spacing      close 
4:    gill-size      narrow 
5:    gill-color      buff 
6: stalk-surface-above-ring      silky 
7: stalk-color-above-ring     cinnamon 
8:  spore-print-color    chocolate,green 
9:    population   scattered,several 

下一次給予在實際r輸出格式,列表或數據幀中的期望的輸出例如

+0

非常感謝。它適合我。 – clairsang

+1

點擊刻度標記 – JeremyS

+0

請問有其他嗎?我不想使用data.table。 – clairsang

相關問題