4
$\begingroup$

I have a Dataset of only 1 column to start with, and I want to StringJoin every element there. Got the error message

StringJoin::string: String expected at position 1 in StringJoin[Dataset [<<33>>]].

Here is code to reproduce:

a = {1, 2, 3}
b = Dataset[ToString /@ a]
c = StringJoin[b]

Tried Normal[StringJoin[b]] & Normal[b], neither worked.

Thanks in advance.

$\endgroup$
  • $\begingroup$ StringJoin[(# & /@ Normal@b)] is this what you're looking for? Edit: Commented before seeing WReach's answer. It's a really nice way of doing this! :) $\endgroup$ – Vahagn Tumanyan May 4 '17 at 16:57
  • $\begingroup$ This solution works, thanks! $\endgroup$ – user1470393 May 4 '17 at 17:14
6
$\begingroup$

StringJoin is not one of the functions that support a dataset argument. We can, however, use StringJoin as a query operator:

b[StringJoin]

(* "123" *)

We can see the functions which do accept a dataset argument by inspecting the output of:

Needs["GeneralUtilities`"]
PrintDefinitions[Dataset]

PrintDefinitions screenshot

The list is quite a bit longer than will fit in a screenshot.

| improve this answer | |
$\endgroup$
5
$\begingroup$

In:

a = {1, 2, 3};
b = Dataset[ToString /@ a];

b[StringJoin, All] (*Method 1, apply StringJoin to the contents of all columns/a specific column*)
StringJoin @@ b (*Method 2, replace Dataset head with StringJoin*)

Out:

"123"
"123"
| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.