I have a dataframe where each column has a unique name, but the content of several columns is identical. The columns with identical content are all factor variables and they end in the same way (e.g. .x or .y). My goal is to join all columns with the same ending (.x or .y) into a single column.
Most solutions I have encountered in this regard combine multiple dataframes, but I have not found a solution yet that does this within a single dataframe. I am providing some example script to illustrate what my dataframe looks like at the moment and the desired output.
# generate some data
dv1 = rnorm(6)
dv2 = rnorm(6)
dv3 = rnorm(6)
# current dataframe
DF <- data.frame(dv1,
iv1.x = sort(rep(letters[1:2], 3)),
iv1.y = as.factor(c(1:6)),
dv2,
iv2.x = sort(rep(letters[1:2], 3)),
iv2.y = as.factor(c(1:6)),
dv3,
iv3.x = sort(rep(letters[1:2], 3)),
iv3.y = as.factor(c(1:6))
)
# desired dataframe
DF.cbmd <- data.frame(dv1,
dv2,
dv3,
iv1.x = sort(rep(letters[1:2], 3)),
iv1.y = as.factor(c(1:6))
)