私はdata
というデータフレームを持っています。私はCRD_NUM
とBIZ_DT
ごとに0と1で新しい列を作成しようとしています。最大Jrny_Ind
を確認し、その行に最大値がある新しい列に1を割り当てます。data.tableを使用して条件に基づいてバイナリ列を作成します。
は、ここで作業するためのデータです:
structure(list(JRNY_ID_NUM = c(115485143065, 115581455926, 115542253339,
115568253504, 115579064996, 115557373723), CRD_NUM = c(1000148004095169,
1000148004095169, 1000148004095169, 1000148004095169, 1000148004095169,
1000148004095169), BIZ_DT = structure(c(3L, 3L, 4L, 4L, 5L, 5L
), .Label = c("01-Jan-17", "02-Jan-17", "03-Jan-17", "04-Jan-17",
"05-Jan-17", "06-Jan-17", "07-Jan-17", "08-Jan-17", "09-Jan-17",
"10-Jan-17", "11-Jan-17", "12-Jan-17", "13-Jan-17", "14-Jan-17",
"15-Jan-17", "16-Jan-17", "17-Jan-17", "18-Jan-17", "19-Jan-17",
"20-Jan-17", "21-Jan-17", "22-Jan-17", "23-Jan-17", "24-Jan-17",
"25-Jan-17", "26-Jan-17", "27-Jan-17", "28-Jan-17", "29-Jan-17",
"30-Jan-17", "31-Jan-17"), class = "factor"), Jrny_Ind = c(1L,
2L, 1L, 2L, 1L, 2L)), .Names = c("JRNY_ID_NUM", "CRD_NUM", "BIZ_DT",
"Jrny_Ind"), class = c("data.table", "data.frame"), row.names = c(NA,
-6L), .internal.selfref = <pointer: 0x0000000002640788>)
所望の出力:
JRNY_ID_NUM CRD_NUM BIZ_DT Jrny_Ind Last_Trip
1: 115485143065 1000148004095169 03-Jan-17 1 0
2: 115581455926 1000148004095169 03-Jan-17 2 1
3: 115542253339 1000148004095169 04-Jan-17 1 0
4: 115568253504 1000148004095169 04-Jan-17 2 1
5: 115579064996 1000148004095169 05-Jan-17 1 0
6: 115557373723 1000148004095169 05-Jan-17 2 1
私は以下のように各カードと日付の "最大行" を取得しようとしている:
data[, .SD[which.max(Jrny_Ind)], by = c("CRD_NUM","BIZ_DT")]
data.table
を使用して新しい列を割り当てる方法がわかりません。
'データ[、last_trip:= +(Jrny_Ind == MAX(Jrny_Ind))=によって、(CRD_NUM、BIZ_DT)]の代わりに、'(または '' as.integer' + ') – Jaap
'ライブラリ(dplyr); %mutation(Last_Trip = as.integer(Jrny_Ind == max(Jrny_Ind))) '%group_by(CRD_NUM、BIZ_DT)%> – Masoud