根据列的位置分配值

时间:2018-02-08 16:22:46

标签: r

我有这样的数据集:

say $subs -> {aa} -> () -> [1] -> [1]

我想创建一个for循环,根据Unique ID#,我想订购date_time列并按升序排列值1。所以它看起来像这样:

ID  Conversion  date_time
1   0          22/08/2016 00:28
1   1          22/08/2016 00:09
2   0          22/08/2016 00:50
2   0          22/08/2016 00:52
2   0          22/08/2016 00:11

提前谢谢

1 个答案:

答案 0 :(得分:0)

使用data.table

df <- read.table(header=T, text="ID  Conversion  date_time
                 1   0          22/08/201600:28
                 1   1          22/08/201600:09
                 2   0          22/08/201600:50
                 2   0          22/08/201600:52
                 2   0          22/08/201600:11")



library(data.table)
setDT(df)
df[, date_time1 := 1:.N, by = ID]
df[, date_time := NULL]
setnames(df, "date_time1", "date_time")

    ID Conversion date_time
1:  1          0         1
2:  1          1         2
3:  2          0         1
4:  2          0         2
5:  2          0         3