擷取邊的型別
在這個練習中,你會把客戶資料框的客戶 ID 與客戶邊列表進行配對,判斷每一條邊是流失(churn)、非流失,或是混合邊。
使用 match() 函式,為邊列表新增兩個欄位。
fromLabel:from欄位節點的流失標籤toLabel:to欄位節點的流失標籤

指令 match(x, y) 會回傳一個向量,表示 x 在 y 中的位置。上圖中,match(edgeList$from, customers$id) 的結果是 1,1,1,2,2。例如,edgeList$from 的第 4 列(ID 為 393 的客戶)是 customers$id 的第 2 個元素。
因此,這位客戶的流失標籤是 customers[2,2],也就是 0。
同理,edgeList$from 中每位客戶的流失標籤為 customers[match(edgeList$from, customers$id),2]。
本練習屬於課程
使用 R 進行網路化資料的預測分析
練習說明
- 在
edgeList資料框中新增名為FromLabel的欄位,做法是將edgeList$from與customers$id配對後,擷取customers$churn作為from節點的標籤。 - 對
to邊也做相同的事,並將欄位命名為ToLabel。 - 在
edgeList資料框中新增名為edgeType的欄位,內容為FromLabel與ToLabel兩欄的和。 - 使用
table()函式查看各種邊型別的數量。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Add the column edgeList$FromLabel
edgeList$FromLabel <- customers[match(edgeList$___, customers$___), 2]
# Add the column edgeList$ToLabel
edgeList$ToLabel <- customers[___(___, ___), 2]
# Add the column edgeList$edgeType
edgeList$edgeType <- edgeList$___ + edgeList$___
# Count the number of each type of edge
___(edgeList$edgeType)