wondering if there is a way to created duplicate rows based upon a value in a cell? For example, in my attached image, I would like to use the value in “No. of Ticks” to create duplicate rows. So row 1018 would be duplicated 9 times.
How about the following method to duplicate records according to the column values?
Step01 : Sample data confirmation
I have the following data, and I duplicate the rows according to the value of dup_int
.The value of dup_int
in A
is 1, so it will stay the same, but the value of dup_int
in B
is 5, so five rows of the same record will be generated.
Step02 : Make function to duplicate
Select Custom R command
and write R script as shown below. Replace dup_int
with No. of Ticks
.
# Write R scrpit
mutate(dup_rows = purrr::map(.x = dup_int, .f = function(x){(rep(1, x))}))
Step03 : Unnest list
According to the value of dup_int
, the list class dup_rows
is generated with 1, so expand it to rows.
Step04 : Checking the data
It is likely that duplicate records are generated according to the value of dup_int
.
I hope this helps.
Thank you sugiaki,
I have tried this but I get the following error message at step 2:
This is my code (note that I renamed the ticks field for simplicity)
Probably, NA
is included in Num_ticks
which specifies duplicate values. In that case, you can complement NA
with 1
to make it work.
Thank you again sugiaki! It works now for me.