如何用最后一个值填充列

时间:2019-09-13 07:53:02

标签: r data.table

  data=structure(list(ID_WORKES = c(119642709L, 119642709L, 119642709L, 
119642709L, 119642709L, 119642709L, 119642709L, 119642709L, 119642709L, 
119642709L, 119642709L), TABL_NOM = c(56220L, 56220L, 56220L, 
56220L, 56220L, 56220L, 56220L, 56220L, 56220L, 56220L, 56220L
), NAME = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L), .Label = "nov", class = "factor"), ID_SP_NAR = c(1048L, 
1049L, 1050L, 1065L, 1066L, 1085L, 1086L, 1087L, 1088L, 1086L, 
1087L), KOD_DOR = c(92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 
92L, 92L), KOD_DEPO = c(13283L, 13283L, 13283L, 13283L, 13283L, 
13283L, 13283L, 13283L, 13283L, 13283L, 13283L), COLUMN_MASH = c(0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 3L, 0L, 4L), x1 = c(0, 0, 0, 0, 0, 
0, 0, 0, 0.0625, 0, 0), x2 = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
2L, 0L, 0L)), .Names = c("ID_WORKES", "TABL_NOM", "NAME", "ID_SP_NAR", 
"KOD_DOR", "KOD_DEPO", "COLUMN_MASH", "x1", "x2"), class = "data.frame", row.names = c(NA, 
-11L))

我只需要使用COLUMN_MASH列,正如我们在这里看到的那样,只有两个值是整数(3和4),另一个值是零。如何为每个id_worker用column_mash中的最后一个值填充column_mash,即在所有行中必须为4。

 ID_WORKES TABL_NOM NAME ID_SP_NAR KOD_DOR KOD_DEPO COLUMN_MASH     x1 x2
1  119642709    56220  nov      1048      92    13283           4 0.0000  0
2  119642709    56220  nov      1049      92    13283           4 0.0000  0
3  119642709    56220  nov      1050      92    13283           4 0.0000  0
4  119642709    56220  nov      1065      92    13283           4 0.0000  0
5  119642709    56220  nov      1066      92    13283           4 0.0000  0
6  119642709    56220  nov      1085      92    13283           4 0.0000  0
7  119642709    56220  nov      1086      92    13283           4 0.0000  0
8  119642709    56220  nov      1087      92    13283           4 0.0000  0
9  119642709    56220  nov      1088      92    13283           4 0.0625  2
10 119642709    56220  nov      1086      92    13283           4 0.0000  0
11 119642709    56220  nov      1087      92    13283           4 0.0000  0

如果最后一个值为0,则用先前的整数值填充列。

3 个答案:

答案 0 :(得分:3)

由于此问题被标记为

library(data.table)
setDT(data)
data[, COLUMN_MASH := last(COLUMN_MASH[COLUMN_MASH != 0]), by = ID_WORKES]

#     ID_WORKES TABL_NOM NAME ID_SP_NAR KOD_DOR KOD_DEPO COLUMN_MASH     x1 x2
#  1: 119642709    56220  nov      1048      92    13283           4 0.0000  0
#  2: 119642709    56220  nov      1049      92    13283           4 0.0000  0
#  3: 119642709    56220  nov      1050      92    13283           4 0.0000  0
#  4: 119642709    56220  nov      1065      92    13283           4 0.0000  0
#  5: 119642709    56220  nov      1066      92    13283           4 0.0000  0
#  6: 119642709    56220  nov      1085      92    13283           4 0.0000  0
#  7: 119642709    56220  nov      1086      92    13283           4 0.0000  0
#  8: 119642709    56220  nov      1087      92    13283           4 0.0000  0
#  9: 119642709    56220  nov      1088      92    13283           4 0.0625  2
# 10: 119642709    56220  nov      1086      92    13283           4 0.0000  0
# 11: 119642709    56220  nov      1087      92    13283           4 0.0000  0

答案 1 :(得分:2)

我们可以将53ave一起使用,即

tail

将其分配回您的数据框以更新with(data, ave(COLUMN_MASH, ID_WORKES, FUN = function(i) tail(i[i != 0], 1))) #[1] 4 4 4 4 4 4 4 4 4 4 4

COLUMN_MASH

答案 2 :(得分:2)

clock = pygame.time.Clock() while main == True: clock.tick(60) # [...] 中,我们可以dplyr group_by并为每个ID_WORKES获得last的非零值。

ID_WORKES