Complete and fill series by decreasing value in group tidyr
I have a fairly complicated issue in which I wish to first complete empty rows and fill in related data. The next step, which is more complicated is to fill the series by decreasing value until we reach a minimum. Here is data for example:
dat <- structure(list(id = 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, 1L
), id2 = c(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), id3 = c("ID_1",
"ID_1", "ID_1", "ID_1", "ID_1", "ID_2", "ID_3", "ID_4", "ID_5",
"ID_6", "ID_7", "ID_8", "ID_9", "ID_10", "ID_11", "ID_12", "ID_12",
"ID_12", "ID_12", "ID_13", "ID_14", "ID_15", "ID_16", "ID_17",
"ID_18", "ID_19"), n_clstr = c(5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 3L, 3L, 2L, 1L, 1L, 1L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 3L, 2L,
1L, 1L), clstr_number = c(1L, 2L, 3L, 4L, 5L, NA, NA, NA, NA,
NA, NA, NA, 1L, 1L, 1L, 1L, 2L, 3L, 4L, NA, NA, NA, NA, NA, 1L,
1L), value = c(0.35, 0.43, 0.51, 0.57, 1, NA, NA, NA, NA, NA,
NA, NA, 1, 1, 1, 0.2, 0.62, 0.79, 1, NA, NA, NA, NA, NA, 1, 1
)), row.names = c(NA, -26L), class = c("tbl_df", "tbl", "data.frame"
), spec = structure(list(cols = list(id = structure(list(), class = c("collector_integer",
"collector")), id2 = structure(list(), class = c("collector_integer",
"collector")), id3 = structure(list(), class = c("collector_character",
"collector")), n_clstr = structure(list(), class = c("collector_integer",
"collector")), clstr_number = structure(list(), class = c("collector_integer",
"collector")), value = structure(list(), class = c("collector_double",
"collector"))), default = structure(list(), class = c("collector_guess",
"collector"))), class = "col_spec"))
So you can see that the data are grouped by nested ID's. For the sake of conciseness, I did not provide an additional id
, but there will be more than one. So the steps that I envision are:
- Group by `c(id, id2, id3)
- Complete clstr_number based on n_cluster and assign
clstr_number = seq(max(n_clstr)-n_clstr, n_clstr,1)
- Fill the corresponding values from above
- As
n_cluster
decreases, fillvalue
with the highestn
values
But I cannot figure out the correct syntax for complete and fill at such a complexity, as well as filling decreasing sets. The final data would look like this:
dat_final <- structure(list(id = 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, 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, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L), id2 = 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, 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),
X3 = c("ID_1", "ID_1", "ID_1", "ID_1", "ID_1", "ID_2", "ID_2",
"ID_2", "ID_2", "ID_2", "ID_3", "ID_3", "ID_3", "ID_3", "ID_3",
"ID_4", "ID_4", "ID_4", "ID_4", "ID_4", "ID_5", "ID_5", "ID_5",
"ID_5", "ID_5", "ID_6", "ID_6", "ID_6", "ID_7", "ID_7", "ID_7",
"ID_8", "ID_8", "ID_9", "ID_10", "ID_11", "ID_12", "ID_12",
"ID_12", "ID_12", "ID_13", "ID_13", "ID_13", "ID_13", "ID_14",
"ID_14", "ID_14", "ID_14", "ID_15", "ID_15", "ID_15", "ID_15",
"ID_16", "ID_16", "ID_16", "ID_16", "ID_17", "ID_18", "ID_19"
), n_clstr = c(5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 3L,
3L, 3L, 3L, 3L, 3L, 2L, 2L, 1L, 1L, 1L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 3L, 3L, 3L, 2L,
2L, 1L, 1L), clstr_number = c(1L, 2L, 3L, 4L, 5L, 1L, 2L,
3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L,
3L, 4L, 5L, 3L, 4L, 5L, 3L, 4L, 5L, 4L, 5L, 1L, 1L, 1L, 1L,
2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L,
2L, 3L, 4L, 3L, 4L, 1L, 1L), value = c(0.35, 0.43, 0.51,
0.57, 1, 0.35, 0.43, 0.51, 0.57, 1, 0.35, 0.43, 0.51, 0.57,
1, 0.35, 0.43, 0.51, 0.57, 1, 0.35, 0.43, 0.51, 0.57, 1,
0.51, 0.57, 1, 0.51, 0.57, 1, 0.57, 1, 1, 1, 1, 0.2, 0.62,
0.79, 1, 0.2, 0.62, 0.79, 1, 0.2, 0.62, 0.79, 1, 0.2, 0.62,
0.79, 1, 0.62, 0.79, 1, 0.79, 1, 1, 1)), row.names = c(NA,
-59L), class = c("tbl_df", "tbl", "data.frame"), spec = structure(list(
cols = list(id = structure(list(), class = c("collector_integer",
"collector")), id2 = structure(list(), class = c("collector_integer",
"collector")), X3 = structure(list(), class = c("collector_character",
"collector")), n_clstr = structure(list(), class = c("collector_integer",
"collector")), clstr_number = structure(list(), class = c("collector_integer",
"collector")), value = structure(list(), class = c("collector_double",
"collector"))), default = structure(list(), class = c("collector_guess",
"collector"))), class = "col_spec"))
r dplyr tidyr fill complete
add a comment |
I have a fairly complicated issue in which I wish to first complete empty rows and fill in related data. The next step, which is more complicated is to fill the series by decreasing value until we reach a minimum. Here is data for example:
dat <- structure(list(id = 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, 1L
), id2 = c(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), id3 = c("ID_1",
"ID_1", "ID_1", "ID_1", "ID_1", "ID_2", "ID_3", "ID_4", "ID_5",
"ID_6", "ID_7", "ID_8", "ID_9", "ID_10", "ID_11", "ID_12", "ID_12",
"ID_12", "ID_12", "ID_13", "ID_14", "ID_15", "ID_16", "ID_17",
"ID_18", "ID_19"), n_clstr = c(5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 3L, 3L, 2L, 1L, 1L, 1L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 3L, 2L,
1L, 1L), clstr_number = c(1L, 2L, 3L, 4L, 5L, NA, NA, NA, NA,
NA, NA, NA, 1L, 1L, 1L, 1L, 2L, 3L, 4L, NA, NA, NA, NA, NA, 1L,
1L), value = c(0.35, 0.43, 0.51, 0.57, 1, NA, NA, NA, NA, NA,
NA, NA, 1, 1, 1, 0.2, 0.62, 0.79, 1, NA, NA, NA, NA, NA, 1, 1
)), row.names = c(NA, -26L), class = c("tbl_df", "tbl", "data.frame"
), spec = structure(list(cols = list(id = structure(list(), class = c("collector_integer",
"collector")), id2 = structure(list(), class = c("collector_integer",
"collector")), id3 = structure(list(), class = c("collector_character",
"collector")), n_clstr = structure(list(), class = c("collector_integer",
"collector")), clstr_number = structure(list(), class = c("collector_integer",
"collector")), value = structure(list(), class = c("collector_double",
"collector"))), default = structure(list(), class = c("collector_guess",
"collector"))), class = "col_spec"))
So you can see that the data are grouped by nested ID's. For the sake of conciseness, I did not provide an additional id
, but there will be more than one. So the steps that I envision are:
- Group by `c(id, id2, id3)
- Complete clstr_number based on n_cluster and assign
clstr_number = seq(max(n_clstr)-n_clstr, n_clstr,1)
- Fill the corresponding values from above
- As
n_cluster
decreases, fillvalue
with the highestn
values
But I cannot figure out the correct syntax for complete and fill at such a complexity, as well as filling decreasing sets. The final data would look like this:
dat_final <- structure(list(id = 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, 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, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L), id2 = 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, 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),
X3 = c("ID_1", "ID_1", "ID_1", "ID_1", "ID_1", "ID_2", "ID_2",
"ID_2", "ID_2", "ID_2", "ID_3", "ID_3", "ID_3", "ID_3", "ID_3",
"ID_4", "ID_4", "ID_4", "ID_4", "ID_4", "ID_5", "ID_5", "ID_5",
"ID_5", "ID_5", "ID_6", "ID_6", "ID_6", "ID_7", "ID_7", "ID_7",
"ID_8", "ID_8", "ID_9", "ID_10", "ID_11", "ID_12", "ID_12",
"ID_12", "ID_12", "ID_13", "ID_13", "ID_13", "ID_13", "ID_14",
"ID_14", "ID_14", "ID_14", "ID_15", "ID_15", "ID_15", "ID_15",
"ID_16", "ID_16", "ID_16", "ID_16", "ID_17", "ID_18", "ID_19"
), n_clstr = c(5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 3L,
3L, 3L, 3L, 3L, 3L, 2L, 2L, 1L, 1L, 1L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 3L, 3L, 3L, 2L,
2L, 1L, 1L), clstr_number = c(1L, 2L, 3L, 4L, 5L, 1L, 2L,
3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L,
3L, 4L, 5L, 3L, 4L, 5L, 3L, 4L, 5L, 4L, 5L, 1L, 1L, 1L, 1L,
2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L,
2L, 3L, 4L, 3L, 4L, 1L, 1L), value = c(0.35, 0.43, 0.51,
0.57, 1, 0.35, 0.43, 0.51, 0.57, 1, 0.35, 0.43, 0.51, 0.57,
1, 0.35, 0.43, 0.51, 0.57, 1, 0.35, 0.43, 0.51, 0.57, 1,
0.51, 0.57, 1, 0.51, 0.57, 1, 0.57, 1, 1, 1, 1, 0.2, 0.62,
0.79, 1, 0.2, 0.62, 0.79, 1, 0.2, 0.62, 0.79, 1, 0.2, 0.62,
0.79, 1, 0.62, 0.79, 1, 0.79, 1, 1, 1)), row.names = c(NA,
-59L), class = c("tbl_df", "tbl", "data.frame"), spec = structure(list(
cols = list(id = structure(list(), class = c("collector_integer",
"collector")), id2 = structure(list(), class = c("collector_integer",
"collector")), X3 = structure(list(), class = c("collector_character",
"collector")), n_clstr = structure(list(), class = c("collector_integer",
"collector")), clstr_number = structure(list(), class = c("collector_integer",
"collector")), value = structure(list(), class = c("collector_double",
"collector"))), default = structure(list(), class = c("collector_guess",
"collector"))), class = "col_spec"))
r dplyr tidyr fill complete
Edited to improve clarity of step 2 to align with desired output.
– user2325155
Nov 13 '18 at 22:38
add a comment |
I have a fairly complicated issue in which I wish to first complete empty rows and fill in related data. The next step, which is more complicated is to fill the series by decreasing value until we reach a minimum. Here is data for example:
dat <- structure(list(id = 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, 1L
), id2 = c(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), id3 = c("ID_1",
"ID_1", "ID_1", "ID_1", "ID_1", "ID_2", "ID_3", "ID_4", "ID_5",
"ID_6", "ID_7", "ID_8", "ID_9", "ID_10", "ID_11", "ID_12", "ID_12",
"ID_12", "ID_12", "ID_13", "ID_14", "ID_15", "ID_16", "ID_17",
"ID_18", "ID_19"), n_clstr = c(5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 3L, 3L, 2L, 1L, 1L, 1L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 3L, 2L,
1L, 1L), clstr_number = c(1L, 2L, 3L, 4L, 5L, NA, NA, NA, NA,
NA, NA, NA, 1L, 1L, 1L, 1L, 2L, 3L, 4L, NA, NA, NA, NA, NA, 1L,
1L), value = c(0.35, 0.43, 0.51, 0.57, 1, NA, NA, NA, NA, NA,
NA, NA, 1, 1, 1, 0.2, 0.62, 0.79, 1, NA, NA, NA, NA, NA, 1, 1
)), row.names = c(NA, -26L), class = c("tbl_df", "tbl", "data.frame"
), spec = structure(list(cols = list(id = structure(list(), class = c("collector_integer",
"collector")), id2 = structure(list(), class = c("collector_integer",
"collector")), id3 = structure(list(), class = c("collector_character",
"collector")), n_clstr = structure(list(), class = c("collector_integer",
"collector")), clstr_number = structure(list(), class = c("collector_integer",
"collector")), value = structure(list(), class = c("collector_double",
"collector"))), default = structure(list(), class = c("collector_guess",
"collector"))), class = "col_spec"))
So you can see that the data are grouped by nested ID's. For the sake of conciseness, I did not provide an additional id
, but there will be more than one. So the steps that I envision are:
- Group by `c(id, id2, id3)
- Complete clstr_number based on n_cluster and assign
clstr_number = seq(max(n_clstr)-n_clstr, n_clstr,1)
- Fill the corresponding values from above
- As
n_cluster
decreases, fillvalue
with the highestn
values
But I cannot figure out the correct syntax for complete and fill at such a complexity, as well as filling decreasing sets. The final data would look like this:
dat_final <- structure(list(id = 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, 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, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L), id2 = 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, 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),
X3 = c("ID_1", "ID_1", "ID_1", "ID_1", "ID_1", "ID_2", "ID_2",
"ID_2", "ID_2", "ID_2", "ID_3", "ID_3", "ID_3", "ID_3", "ID_3",
"ID_4", "ID_4", "ID_4", "ID_4", "ID_4", "ID_5", "ID_5", "ID_5",
"ID_5", "ID_5", "ID_6", "ID_6", "ID_6", "ID_7", "ID_7", "ID_7",
"ID_8", "ID_8", "ID_9", "ID_10", "ID_11", "ID_12", "ID_12",
"ID_12", "ID_12", "ID_13", "ID_13", "ID_13", "ID_13", "ID_14",
"ID_14", "ID_14", "ID_14", "ID_15", "ID_15", "ID_15", "ID_15",
"ID_16", "ID_16", "ID_16", "ID_16", "ID_17", "ID_18", "ID_19"
), n_clstr = c(5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 3L,
3L, 3L, 3L, 3L, 3L, 2L, 2L, 1L, 1L, 1L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 3L, 3L, 3L, 2L,
2L, 1L, 1L), clstr_number = c(1L, 2L, 3L, 4L, 5L, 1L, 2L,
3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L,
3L, 4L, 5L, 3L, 4L, 5L, 3L, 4L, 5L, 4L, 5L, 1L, 1L, 1L, 1L,
2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L,
2L, 3L, 4L, 3L, 4L, 1L, 1L), value = c(0.35, 0.43, 0.51,
0.57, 1, 0.35, 0.43, 0.51, 0.57, 1, 0.35, 0.43, 0.51, 0.57,
1, 0.35, 0.43, 0.51, 0.57, 1, 0.35, 0.43, 0.51, 0.57, 1,
0.51, 0.57, 1, 0.51, 0.57, 1, 0.57, 1, 1, 1, 1, 0.2, 0.62,
0.79, 1, 0.2, 0.62, 0.79, 1, 0.2, 0.62, 0.79, 1, 0.2, 0.62,
0.79, 1, 0.62, 0.79, 1, 0.79, 1, 1, 1)), row.names = c(NA,
-59L), class = c("tbl_df", "tbl", "data.frame"), spec = structure(list(
cols = list(id = structure(list(), class = c("collector_integer",
"collector")), id2 = structure(list(), class = c("collector_integer",
"collector")), X3 = structure(list(), class = c("collector_character",
"collector")), n_clstr = structure(list(), class = c("collector_integer",
"collector")), clstr_number = structure(list(), class = c("collector_integer",
"collector")), value = structure(list(), class = c("collector_double",
"collector"))), default = structure(list(), class = c("collector_guess",
"collector"))), class = "col_spec"))
r dplyr tidyr fill complete
I have a fairly complicated issue in which I wish to first complete empty rows and fill in related data. The next step, which is more complicated is to fill the series by decreasing value until we reach a minimum. Here is data for example:
dat <- structure(list(id = 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, 1L
), id2 = c(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), id3 = c("ID_1",
"ID_1", "ID_1", "ID_1", "ID_1", "ID_2", "ID_3", "ID_4", "ID_5",
"ID_6", "ID_7", "ID_8", "ID_9", "ID_10", "ID_11", "ID_12", "ID_12",
"ID_12", "ID_12", "ID_13", "ID_14", "ID_15", "ID_16", "ID_17",
"ID_18", "ID_19"), n_clstr = c(5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 3L, 3L, 2L, 1L, 1L, 1L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 3L, 2L,
1L, 1L), clstr_number = c(1L, 2L, 3L, 4L, 5L, NA, NA, NA, NA,
NA, NA, NA, 1L, 1L, 1L, 1L, 2L, 3L, 4L, NA, NA, NA, NA, NA, 1L,
1L), value = c(0.35, 0.43, 0.51, 0.57, 1, NA, NA, NA, NA, NA,
NA, NA, 1, 1, 1, 0.2, 0.62, 0.79, 1, NA, NA, NA, NA, NA, 1, 1
)), row.names = c(NA, -26L), class = c("tbl_df", "tbl", "data.frame"
), spec = structure(list(cols = list(id = structure(list(), class = c("collector_integer",
"collector")), id2 = structure(list(), class = c("collector_integer",
"collector")), id3 = structure(list(), class = c("collector_character",
"collector")), n_clstr = structure(list(), class = c("collector_integer",
"collector")), clstr_number = structure(list(), class = c("collector_integer",
"collector")), value = structure(list(), class = c("collector_double",
"collector"))), default = structure(list(), class = c("collector_guess",
"collector"))), class = "col_spec"))
So you can see that the data are grouped by nested ID's. For the sake of conciseness, I did not provide an additional id
, but there will be more than one. So the steps that I envision are:
- Group by `c(id, id2, id3)
- Complete clstr_number based on n_cluster and assign
clstr_number = seq(max(n_clstr)-n_clstr, n_clstr,1)
- Fill the corresponding values from above
- As
n_cluster
decreases, fillvalue
with the highestn
values
But I cannot figure out the correct syntax for complete and fill at such a complexity, as well as filling decreasing sets. The final data would look like this:
dat_final <- structure(list(id = 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, 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, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L), id2 = 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, 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),
X3 = c("ID_1", "ID_1", "ID_1", "ID_1", "ID_1", "ID_2", "ID_2",
"ID_2", "ID_2", "ID_2", "ID_3", "ID_3", "ID_3", "ID_3", "ID_3",
"ID_4", "ID_4", "ID_4", "ID_4", "ID_4", "ID_5", "ID_5", "ID_5",
"ID_5", "ID_5", "ID_6", "ID_6", "ID_6", "ID_7", "ID_7", "ID_7",
"ID_8", "ID_8", "ID_9", "ID_10", "ID_11", "ID_12", "ID_12",
"ID_12", "ID_12", "ID_13", "ID_13", "ID_13", "ID_13", "ID_14",
"ID_14", "ID_14", "ID_14", "ID_15", "ID_15", "ID_15", "ID_15",
"ID_16", "ID_16", "ID_16", "ID_16", "ID_17", "ID_18", "ID_19"
), n_clstr = c(5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 3L,
3L, 3L, 3L, 3L, 3L, 2L, 2L, 1L, 1L, 1L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 3L, 3L, 3L, 2L,
2L, 1L, 1L), clstr_number = c(1L, 2L, 3L, 4L, 5L, 1L, 2L,
3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L,
3L, 4L, 5L, 3L, 4L, 5L, 3L, 4L, 5L, 4L, 5L, 1L, 1L, 1L, 1L,
2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L,
2L, 3L, 4L, 3L, 4L, 1L, 1L), value = c(0.35, 0.43, 0.51,
0.57, 1, 0.35, 0.43, 0.51, 0.57, 1, 0.35, 0.43, 0.51, 0.57,
1, 0.35, 0.43, 0.51, 0.57, 1, 0.35, 0.43, 0.51, 0.57, 1,
0.51, 0.57, 1, 0.51, 0.57, 1, 0.57, 1, 1, 1, 1, 0.2, 0.62,
0.79, 1, 0.2, 0.62, 0.79, 1, 0.2, 0.62, 0.79, 1, 0.2, 0.62,
0.79, 1, 0.62, 0.79, 1, 0.79, 1, 1, 1)), row.names = c(NA,
-59L), class = c("tbl_df", "tbl", "data.frame"), spec = structure(list(
cols = list(id = structure(list(), class = c("collector_integer",
"collector")), id2 = structure(list(), class = c("collector_integer",
"collector")), X3 = structure(list(), class = c("collector_character",
"collector")), n_clstr = structure(list(), class = c("collector_integer",
"collector")), clstr_number = structure(list(), class = c("collector_integer",
"collector")), value = structure(list(), class = c("collector_double",
"collector"))), default = structure(list(), class = c("collector_guess",
"collector"))), class = "col_spec"))
r dplyr tidyr fill complete
r dplyr tidyr fill complete
edited Nov 13 '18 at 22:37
user2325155
asked Nov 13 '18 at 22:18
user2325155user2325155
6219
6219
Edited to improve clarity of step 2 to align with desired output.
– user2325155
Nov 13 '18 at 22:38
add a comment |
Edited to improve clarity of step 2 to align with desired output.
– user2325155
Nov 13 '18 at 22:38
Edited to improve clarity of step 2 to align with desired output.
– user2325155
Nov 13 '18 at 22:38
Edited to improve clarity of step 2 to align with desired output.
– user2325155
Nov 13 '18 at 22:38
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53290372%2fcomplete-and-fill-series-by-decreasing-value-in-group-tidyr%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53290372%2fcomplete-and-fill-series-by-decreasing-value-in-group-tidyr%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Edited to improve clarity of step 2 to align with desired output.
– user2325155
Nov 13 '18 at 22:38