I have a data.table that looks like this:
require("data.table")
dt1 <- data.table(VAR1 = c("Brick","Sand","Concrete","Stone"), VAR2 = c(100,23,76,43), VAR3 = c("Place","Location","Place","Vista"), VAR4 = c("Place","Tree","Wood","Vista"), VAR5 = c("Place","Tree","Wood","Forest"))
I would like to paste named columns (my real data has additional columns) together in this order: VAR2, VAR1, VAR3, VAR4 and VAR5. However, I have two conditions:
- Values in the same row should not be duplicated (when values are duplicated the column with the last entry should be the one kept - so in my example 'Place' in VAR5 would be the one retained)
- A comma should be a separator when pasting except for between VAR2 and VAR1
My expected output would look like this:
dt2 <- data.table(VAR6 = c("100 Brick, Place","23 Sand, Location, Tree","76 Concrete, Place, Wood","43 Stone, Vista, Forest"))