Assuming your data starts in A1 I would put the following in column B:
B1:
=A1
B2:
=B1&","&A2
You can then paste column B2 down the whole column. The last cell in column B should now be a comma separated list of column A.
Answer from Sux2Lose on Stack ExchangeVideos
Can I use a separator other than a comma?
Can I remove duplicates and sort the list?
Will it handle extra blank lines and spaces?
Assuming your data starts in A1 I would put the following in column B:
B1:
=A1
B2:
=B1&","&A2
You can then paste column B2 down the whole column. The last cell in column B should now be a comma separated list of column A.
- Copy the column in Excel
- Open Word
- "Paste special" as text only
- Select the data in Word (the one that you need to convert to text separated with
,), press Ctrl-H (Find & replace) - In "Find what" box type
^p - In "Replace with" box type
, - Select "Replace all"
The letters vector is in-built in R, and paste0 allows you to "collapse" vectors with specified separation characters, in this case a comma:
> paste0(letters[1:5], collapse=",")
[1] "a,b,c,d,e"
It wasn't clear what form these letters had in a dataframe, but if they were simply a column named, say letts, you could do this:
paste0( dfrm$letts, collapse=",")
I assume that what you really want is the string "'a', 'b', 'c', 'd'". We can use shQuote to quote the elements and toString to turn them into a single comma separated string. Finally insert it into the SQL statement using sprintf:
lets <- c("a", "b", "c", "d")
s <- toString(shQuote(lets))
s
## [1] "'a', 'b', 'c', 'd'"
sql <- sprintf("select * from mytab where mycol in (%s)", s)
sql
## [1] "select * from mytab where mycol in ('a', 'b', 'c', 'd')"
You can also use fn$ from the gsubfn package to perform the insertion. sqldf automatically loads gsubfn so this works using s from above:
library(sqldf)
fn$sqldf("select 'b' in ($s) as isPresent")
## isPresent
## 1 1