I have a data frame that has a number of variables in it that I want to concatenate into new variables in that same data frame. A simplified version of my data frame df looks like this:
first.1 second.1 first.2 second.2
1222 3223 3333 1221
1111 2212 2232 2113
Here is how I do it inefficiently without a for loop:
df$concatenated.1 <- paste0(df$first.1,"-",df$second.1)
df$concatenated.2 <- paste0(df$first.2,"-",df$second.2)
Which results in the following data frame df:
first.1 second.1 first.2 second.2 concatenated.1 concatenated.2
1222 3223 3333 1221 1222-3223 3333-1221
1111 2212 2232 2113 1111-2212 2232-2113
I have a lot more than 2 pairs of variables to concatenate, so I would like to do this in a for loop:
for (i in 1:2){
??
}
Any ideas on how to accomplish this?