In this post, we will see how to resolve Updating integer within string row for each instance in R dataframe
Question:
I have a dataframe similar to the following reproducible one in which one column contains HTML code:
I would like to update the integer within the square brackets of the
HTML
column to reflect each instance of repeat. For example, the second time that [demography_form] appears in a row, I would like the square brackets following it to be 2:
What’s the best way of going about doing this? I was thinking of somehow creating an instance column and then using that to update the value in the square brackets, deleting it at the end? Thanks in advance.
Best Answer:
Create a grouping column from the substring inside the[]
from HTML column, replace the digits inside the []
with the sequence of rows (row_number()
) using str_replace_all
If you have better answer, please add a comment about this, thank you!
Source: Stackoverflow.com