我和R et Rstudio一起工作,我遇到了问题。
首先,我有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
和
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 :(得分: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输出格式,列表或数据帧提供所需的输出,例如