2014-03-13 80 views
1

我有一個看起來像這樣的數據表:編輯變量

> head(dput(dt)) 
structure(list(A = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L 
), T = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 
14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 1L, 
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 
16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 
29L, 30L, 31L, 32L, 33L), X = c(26.91, 23.92, 23.92, 23.92, 23.92, 
23.92, 23.92, 23.92, 23.92, 20.93, 20.93, 20.93, 20.93, 20.93, 
20.93, 20.93, 20.93, 20.93, 20.93, 20.93, 29.9, 20.93, 14.95, 
8.97, 8.97, 858.13, 861.12, 858.13, 858.13, 858.13, 858.13, 858.13, 
858.13, 858.13, 858.13, 858.13, 858.13, 858.13, 858.13, 858.13, 
858.13, 858.13, 858.13, 858.13, 858.13, 858.13, 858.13, 858.13, 
858.13, 858.13, 858.13, 858.13, 858.13, 858.13, 858.13, 858.13, 
858.13, 858.13), Y = c(167.44, 167.44, 164.45, 164.45, 164.45, 
164.45, 164.45, 164.45, 164.45, 143.52, 143.52, 143.52, 143.52, 
143.52, 143.52, 143.52, 143.52, 143.52, 143.52, 143.52, 176.41, 
182.39, 185.38, 188.37, 188.37, 257.14, 257.14, 257.14, 257.14, 
257.14, 257.14, 257.14, 257.14, 257.14, 257.14, 257.14, 257.14, 
257.14, 257.14, 257.14, 257.14, 257.14, 257.14, 257.14, 257.14, 
257.14, 257.14, 257.14, 257.14, 257.14, 257.14, 257.14, 257.14, 
257.14, 257.14, 257.14, 257.14, 260.13), V = c(2.99, 2.99, 0, 
0, 0, 0, 0, 0, 21.142, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 34.091, 
10.781, 6.6858, 6.6858, 0, 2.99, 2.99, 2.99, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 2.99, 0), P = c(180, -90, 0, 0, 0, 0, 0, 0, -98.13, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 74.745, 146.31, 153.43, 153.43, 0, 
180, 0, 180, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 90, 0)), .Names = c("A", 
"T", "X", "Y", "V", "P"), row.names = c(NA, -58L), class = c("data.table", 
"data.frame"), sorted = "A", .internal.selfref = <pointer: 0x0000000000320788>) 

> head(dt[dt$A==1, ]) 
    A T  X  Y V P 
1: 1 1 26.91 167.44 2.99 180 
2: 1 2 23.92 167.44 2.99 -90 
3: 1 3 23.92 164.45 0.00 0 
4: 1 4 23.92 164.45 0.00 0 
5: 1 5 23.92 164.45 0.00 0 
6: 1 6 23.92 164.45 0.00 0 

> tail(dt[dt$A==1, ]) 
    A T  X  Y  V  P 
1: 1 20 20.93 143.52 34.0910 74.745 
2: 1 21 29.90 176.41 10.7810 146.310 
3: 1 22 20.93 182.39 6.6858 153.430 
4: 1 23 14.95 185.38 6.6858 153.430 
5: 1 24 8.97 188.37 0.0000 0.000 
6: 1 25 8.97 188.37 2.9900 180.000 

它是由A.分類我想要做的就是編輯V和P列(不整排)。在這兩方面,我想:

  1. 刪除最後一個值
  2. 傳遞從第一行到第2的值,等等..
  3. 使第1個數值= 0

所以,最後我想要一個看起來像這樣的數據表。

> head(dt[dt$A==1, ]) 
    A T  X  Y V P 
1: 1 1 26.91 167.44 2.99 0 
2: 1 2 23.92 167.44 2.99 180 
3: 1 3 23.92 164.45 0.00 -90 
4: 1 4 23.92 164.45 0.00 0 
5: 1 5 23.92 164.45 0.00 0 
6: 1 6 23.92 164.45 0.00 0 

> tail(dt[dt$A==1, ]) 
    A  X  Y  V  P 
1: 20 20.93 143.52 0.0000 0.000 
2: 21 29.90 176.41 34.0910 74.745 
3: 22 20.93 182.39 10.7810 146.310 
4: 23 14.95 185.38 6.6858 153.430 
5: 24 8.97 188.37 6.6858 153.430 
6: 25 8.97 188.37 0.0000 0.000 

我需要做的是在數據表中,我想我的數據排序分成A(這我還可以採用分體式做),但我也堅持瞭如何做三件事情,我想做。

謝謝。

回答

3

這將做到這一點:

dt[, P := c(0, head(P, -1)), by = A] 

另外請注意,您不需要內[.data.table指定dt$,即使用dt[A == 1],而不是OP。

+0

有什麼機會可以擴展這實際上做什麼? –

+3

@AndyClifton'c(0,head(P,-1))'是需要的移位操作,然後上面將這個分配給'P',對於每個'by'組 – eddi

+0

謝謝@eddi。我總是忘記''s''可以在'data.table'中跳過。 – Kaye11