2016-12-16 322 views
0

輸入新列:骨料與R

Time,id1,id2 
22:30,1,0 
22:32,2,1 
22:33,1,0 
22:34,2,1 

輸出通過我的代碼期望

Time,Time2,id1,id2 
22:30,22:33,1,0 
22:32,22:34,2,1 

輸出

Time,id1,id2 
22:30,22:33,1,0 
22:32,22:34,2,1 

什麼變化,我應該讓我的代碼aggregate(Time~,df,FUN=toString) 我ID1和id2在一起是關鍵,時間是每個關鍵的進出時間。我需要抽出時間和時間作爲單獨的列值。目前他們在時間欄中。

我也試過用awk。

+0

無法使用,因爲有限正確的格式通過2G移動連接。請不要投票 – user1977867

+0

'aggregate(Time〜。,df,FUN = toString)' – Sotos

+0

@Sotos就是我已經在使用的 – user1977867

回答

0

如果你不希望使用任何套餐,這將工作:

df <- aggregate(Time~.,df,FUN=toString) 
df 
#output 
id1 id2   Time 
1 0 22:30, 22:33 
2 1 22:32, 22:34 

df$Time2 <- lapply(strsplit(as.character(df$Time), ","),"[", 2) 
df$Time <- lapply(strsplit(as.character(df$Time), ","),"[", 1) 
df 
#output 
id1 id2 Time Time2 
1 0 22:30 22:33 
2 1 22:32 22:34 
0

隨着awk

$ cat time.awk 
BEGIN { 
    FS = OFS = "," 
} 

function in_time() { 
    n++ 
    store[id1, id2] = n 
    itime[n] = time; iid1[n] = id1; iid2[n] = id2 
} 

function out_time( i) { 
    i = store[id1, id2] 
    otime[i] = time 
} 


NR > 1 { 
    time = $1; id1 = $2; id2 = $3 
    if ((id1, id2) in store) out_time() 
    else      in_time() 
} 

END { 
    print "Time,id1,id2" 
    for (i = 1; i <= n; i++) 
     print itime[i], otime[i], iid1[i], iid2[i] 
} 

用法:

awk -f time.awk file.dat