我想在我的列中添加负序列或正序列。实际上,对于每个ID,如果col_int==1,则在值1之前的行上放置负序列,并在值1之后的行上放置负序列。
dfs <- data.frame(ID = c(rep("ID1",11),rep("ID2",4), rep("ID3",6)) , col_int=c(c(0,0,0,0,0,1,0,0,0,0,0),c(1,0,0,0),c(0,0,1,0,1,1) )) dfs_out <- dfs %>% mutate(col_out = c(c(seq(-5,-1)),1,c(seq(1,5)), c(1,1,2,3),c(-2,-1,1,1,2,3)))
1条答案
按热度按时间wbrvyc0a1#
试试看