0
我有以下數據表R:在R數據表中的特定位置插入一列
user_id user_system_days event total_pics
[1,] 21196680 0 0 124
[2,] 21197699 0 0 4
[3,] 21197861 0 0 14
[4,] 21198820 1 0 21
[5,] 21199601 0 0 3
[6,] 21203847 0 0 1
基於列的事件我想插入一列數據表中的值,我的方法是通過編寫一個函數,並使用該函數返回一個值。
opposite_event <- function(x) {
y <- 1
if (x == 1)
y <- 0
return (y)
}
我打電話的功能如下:
dt[,op_event:=opposite_event(dt$event)]
這實際上增加了列,但價值是錯誤的:
user_id user_system_days event total_pics op_event
[1,] 21196680 0 0 124 0
[2,] 21197699 0 0 4 0
[3,] 21197861 0 0 14 0
[4,] 21198820 1 0 21 0
[5,] 21199601 0 0 3 0
[6,] 21203847 0 0 1 0
根據我的職能,如果dt$event
的價值1
然後返回值應爲0
,如果dt$event
爲0,則返回值應爲1
另外我不確定如何將該列插入到特定位置,例如事件列之後。
應該工作,例如,如果事件== 1,則op_event的值爲1,如果事件== 0,那麼op_event應該是1 –
@空假設是。 –
@SeñorO什麼是重新安排列順序的最佳方式,例如在事件列後獲取列op_event? –