Search code examples
rrow

Replicate some rows in R, keeping some values constant and changing others


I have a dataset composed of political parties and their corresponding ideology. Some of these parties have changed their name, but the database does not have these renamed parties. What I want to do is duplicate rows corresponding to political parties which were renamed, keeping their ideological values constant but changing their names accordingly.

For example. Suppose I generate the following data.frame:

df <- data.frame(party = c("AB", "PQ", "HL", "AS"), ideology = c("left", "center-right", "right", "right"))

Now suppose that the party AB changed its name to RB. I want to create a new row with a party called RB and its ideology value corresponding to AB's ideology value.

How do I do this in an efficient way? In the real dataset, I have several parties that were renamed.


Solution

  • You can create a data.frame holding the old and the new name, extract the present information using match, overwrite the party name and use rbind to join the new data to the present.

    x <- data.frame(old="AB", new="RB")
    
    y <- df[match(x$old, df$party),]
    y$party <- x$new
    rbind(df, y)
    
    #  party     ideology
    #1    AB         left
    #2    PQ center-right
    #3    HL        right
    #4    AS        right
    #5    RB         left