Reputation: 1237
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:
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"))
Upvotes: 3
Views: 877
Reputation: 887901
We can use do.call(paste
after selecting the column in the order in .SDcols
, removve the duplicate words with a regex expression
dt1[, .(VAR6 = sub(",", " ", gsub("\\b(\\w+)\\b\\s*,\\s*(?=.*\\1)", "",
do.call(paste, c(.SD, sep=",")), perl = TRUE))),
.SDcols = names(dt1)[c(2:1, 3:5)]]
# VAR6
#1: 100 Brick,Place
#2: 23 Sand,Location,Tree
#3: 76 Concrete,Place,Wood
#4: 43 Stone,Vista,Forest
or group by the sequence of rows and do the paste
V6 <- dt1[, sprintf("%s %s, %s", VAR2, VAR1,
toString(unique(unlist(.SD)))), 1:nrow(dt1), .SDcols = VAR3:VAR5]$V1
data.table(V6)
# V6
#1: 100 Brick, Place
#2: 23 Sand, Location, Tree
#3: 76 Concrete, Place, Wood
#4: 43 Stone, Vista, Forest
Upvotes: 4