私がdfを持っている場合:
> ID<-c("A","A","A","B","B","B","B","C","C","C","C")
> attr<-c("yes1","yes1","no","yes2","yes1","yes1","yes1","no","no","yes1","yes2")
> df = data.frame(ID, attr) ; df
ID attr
1 A yes1
2 A yes1
3 A no
4 B yes2
5 B yes1
6 B yes1
7 B yes1
8 C no
9 C no
10 C yes1
11 C yes2
何千ものIDがあります。IDごとの属性の割合を出力する別の列を追加したいと思い"yes"
ます。属性が1つしかない場合も同様です"no"
。
ID %yes #no
1 A 66.7 1
2 B 100 0
3 C 50 2
SQLに似た行を統合する方法はありますGROUP BY
か?最終的に、この新しいdfはIDを分類し、元のdfに追加されます。
ID attr result
1 A yes1 Pos
2 A yes1 Pos
3 A no False
4 B yes2 TruePos
5 B yes1 TruePos
6 B yes1 TruePos
7 B yes1 TruePos
8 C no False
9 C no False
10 C yes1 Pos
11 C yes2 Pos