I am looking for a function to concatenate two strings inside SQLDF in R, that works like paste(), but could not find any. The reason for doing that is I want to concatenate two columns while joining two data frames. Instead of using merge() to do the join then use paste(), I sometimes want to use sqldf().
Asked
Active
Viewed 9,517 times
1 Answers
10
Just use the syntax for concatenation in SQL, e.g.,
d <- data.frame(x = c("a", "b"), y = c("1", "2"))
sqldf("select *, x||y from d")
# x y x||y
# 1 a 1 a1
# 2 b 2 b2

Ista
- 10,139
- 2
- 37
- 38
-
how do you add a separator - so final result would be `a-1` instead of `a1`? – nak5120 Mar 07 '19 at 16:47
-
@nak5120 just add it in: `sqldf("select *, x||'-'||y from d")` – Ista Mar 07 '19 at 17:10