In data frame evdf
how to assign number such that it increases either by 1 after every zero in ev
column or when there is a change/gap in the date (Date
) column. i.e., I want to create a column like outevdf
in outevdf
.
It may be too simple but could not figure out :(.
evdf<-structure(list(Date = structure(c(16598, 16599, 16600, 16601,
16602, 16603, 16604, 16605, 16606, 16607, 16610, 16611, 16612,
16613, 16614, 16615, 16616, 16617, 16618, 16619, 16620, 16621,
16622, 16623, 16624, 16631, 16632, 16633), class = "Date"), ev = c(1,
1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1)), row.names = c(NA, -28L), class = c("tbl_df",
"tbl", "data.frame"))
evdf
Expected output
outevdf<-structure(list(Date = structure(c(16598, 16599, 16600, 16601,
16602, 16603, 16604, 16605, 16606, 16607, 16610, 16611, 16612,
16613, 16614, 16615, 16616, 16617, 16618, 16619, 16620, 16621,
16622, 16623, 16624, 16631, 16632, 16633), class = "Date"), ev = c(1,
1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1), outevdf = c(1, 1, 1, 1, 1, 2, 2, 2, 2, 2,
3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5)), row.names = c(NA,
-28L), spec = structure(list(cols = list(Date = structure(list(), class = c("collector_character",
"collector")), ev = structure(list(), class = c("collector_double",
"collector")), outevdf = structure(list(), class = c("collector_double",
"collector"))), default = structure(list(), class = c("collector_guess",
"collector")), skip = 1L), class = "col_spec"), class = c("spec_tbl_df",
"tbl_df", "tbl", "data.frame"))
outevdf
Follwoing @badalovi answer, when i implement in larger dataset, the increment is not continuous. See
evdf2 <-structure(list(Date = structure(c(16191, 16192, 16193, 16194,
16195, 16201, 16202, 16203, 16204, 16205, 16206, 16207, 16208,
16211, 16212, 16213, 16214, 16215, 16216, 16217, 16218, 16219,
16220, 16221, 16222, 16223, 16224, 16225, 16226, 16227, 16228,
16229, 16230, 16231, 16232, 16233, 16234, 16235, 16236, 16237,
16238, 16239, 16240, 16241, 16242, 16243, 16244, 16245, 16246,
16247, 16248, 16249, 16250, 16252, 16253, 16254, 16255, 16256,
16257, 16258, 16259, 16260, 16261, 16262, 16263, 16264, 16265,
16266, 16267, 16268, 16269, 16270, 16271, 16274, 16275, 16276,
16277, 16278, 16279, 16280, 16281, 16282, 16283, 16284, 16285,
16286, 16287, 16293, 16294, 16295, 16296, 16297, 16298, 16299,
16300, 16301, 16302, 16303, 16304, 16305, 16306, 16307, 16308,
16309, 16310, 16311, 16312, 16313, 16314, 16315, 16316, 16317,
16318, 16319, 16320, 16321, 16322, 16323, 16324, 16325, 16326,
16327, 16328, 16331, 16332, 16333, 16334, 16335, 16336, 16337,
16338, 16339, 16340, 16341, 16342), class = "Date"), ev = c(1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1,
1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1), outev = c(1, 1, 1, 1, 1, 2, 2, 2, 2,
2, 2, 2, 2, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5,
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7,
7, 7, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
9, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 13,
13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13,
13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13,
13, 13, 13, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15)), row.names = c(NA,
-135L), class = c("tbl_df", "tbl", "data.frame"))
question from:
https://stackoverflow.com/questions/65949285/assign-event-number-based-on-change-location-or-date-in-r-dataframe