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$
2
  • $\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$ May 4, 2017 at 16:57
  • $\begingroup$ This solution works, thanks! $\endgroup$ May 4, 2017 at 17:14

2 Answers 2

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.

$\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"
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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