[R] Breaking down a list into a table

Bert Gunter bgunter.4567 at gmail.com
Wed Nov 30 17:08:08 CET 2016

Well, if i understand correctly, there's really no need to use special
packages for such simple tasks. The strsplit() function in base R does
nicely (see ?strplit):

Here is a reproducible example, --which you did not provide; please do
so in future:

> testdat <- c("abc;def;hij","qq;sdff;kuqw","oprrt;lbas;rw")

> testdat
[1] "abc;def;hij"   "qq;sdff;kuqw"  "oprrt;lbas;rw"

## note that this is a vector, not an array/data frame with 1 column.
You can subscript it in the call below -- e.g. testdat[,1] -- if it is
an array.

> out <- strsplit(testdat,split= ";")
> out
[1] "abc" "def" "hij"

[1] "qq"   "sdff" "kuqw"

[1] "oprrt" "lbas"  "rw"

## Note that the output of strsplit() is a list because it is designed
to handle the more general case of unequal numbers of pieces in each
row. To reform it to a matrix (i.e. 2d array) use rbind in a do.call
(?do.call) construct:

> do.call(rbind,out)
     [,1]    [,2]   [,3]
[1,] "abc"   "def"  "hij"
[2,] "qq"    "sdff" "kuqw"
[3,] "oprrt" "lbas" "rw"

All of this presupposes familiarity with basic R data structures and
constructs. If you do not have such familiarity yet, then I think you
would do well to go through some R tutorials to gain it (if you wish
to continue to use R). Packages are nice, with many helpful features,
but you still need to know the basics of the language -- and R *is* a

As far as your error message is concerned, separate() is apparently an
S3 generic function (more reading!) but has no method for matrices. If
you had done the call as:

output<-separate(into,input[,1] ,into=c("a","b","c","d"),sep=";")

## giving the data as a vector rather than an array of 1 column
it probably would have worked (but you need to check, as I don't use
the tidyr package).


-- Bert

Bert Gunter

"The trouble with having an open mind is that people keep coming along
and sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )

On Wed, Nov 30, 2016 at 7:18 AM, Ferri Leberl <ferri.leberl at gmx.at> wrote:
> Thank to Ulrik for the hint.
> However, I don't comprehend the function until now:
> For example I made up an array "input":
> input
>      kop
> [1,] "w;d;e;f"
> [2,] "w;d;e;f"
> [3,] "w;d;e;f"
> [4,] "w;d;e;f"
> [5,] "w;d;e;f"
> and tried to break it into four cols with commmand:
> output<-separate(into,kop,into=c("a","b","c","d"),sep=";")
> R returned:
> Fehler in UseMethod("separate_") :
>   nicht anwendbare Methode für 'separate_' auf Objekt der Klasse "c('matrix', 'character')" angewendet
> Could you please explain me my mistake?
> Thank you in advance!
> Yours, Ferri
> Gesendet: Dienstag, 22. November 2016 um 14:57 Uhr
> Von: "Ulrik Stervbo" <ulrik.stervbo at gmail.com>
> An: "Ferri Leberl" <ferri.leberl at gmx.at>, "r-helpr-project.org" <r-help at r-project.org>
> Betreff: Re: [R] Breaking down a list into a table
> Hi Ferri,
>  It sounds like the function 'separate' from the tidyr package is what you look for,
> Ulrik
> On Tue, 22 Nov 2016 at 14:49 Ferri Leberl <ferri.leberl at gmx.at[mailto:ferri.leberl at gmx.at]> wrote:
> Dear All,
> I asked for support to deal with a hirarchy within a character separated list.
> I solved the problem crudely but effectively by
> - Choosing for a TSV as input, where in columns that may contain several (or as well no) items the items are separated via semicolon
> - adding semicolons to the first row to grant that the first row has the maximum number of semicolons of this column
> - grasping the column(x<-myarray[,y], where y is some integer value) and saving it as a TSV (with only one column)
> - importing it again, defining it semicolumn-separated, with fill option
> To all those who feel pain reading this: Is there a shortcut?
> Thank you in advance.
> Yours, Ferri
> ______________________________________________
> R-help at r-project.org[mailto:R-help at r-project.org] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help[https://stat.ethz.ch/mailman/listinfo/r-help]
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html[http://www.R-project.org/posting-guide.html]
> and provide commented, minimal, self-contained, reproducible code.
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

More information about the R-help mailing list