[R] Creating matrix from long table in database (pivoting)

Henrique Dallazuanna wwwhsd at gmail.com
Tue Mar 2 19:49:44 CET 2010


Try this:

DF <- read.table(textConnection("1 this
1 is
1 the
1 first
1 row
2 this
2 is
2 the
2 send
2 row"))
reshape(DF, v.names = 'V2', idvar = 'V1', timevar = 'V2', direction = 'wide')

On Tue, Mar 2, 2010 at 3:35 PM, Jan Hornych <jh.hornych at gmail.com> wrote:
> Hi all,
>
> I have a table in database that is very long and when simplified it has only
> two columns in it (id, text). id is the row, and text is the column.
> Technically the text is a term and and id is the document.
> If simplifying this and assuming there is only one occurrence of the term
> per the document. I shall be able to convert this into a binary matrix.
> Table looks like this...
>
> *ID** **Text*
> ------------
> 1 this
> 1 is
> 1 the
> 1 first
> 1 row
> 2 this
> 2 is
> 2 the
> 2 send
> 2 row
> ...
>
>
> in R I would like to have it as
>
> *id  this is the first second row*
> ------------------------------------------------
> 1     1  1   1     1          0     1
> 2     1  1   1     0          1     1
>
> it would be simpler for me to do this transformation in R as I guess the
> language is more handy as the SQL. The table in R have few dozen thousand of
> columns and rows as well. I know how to read the data from database, but
> just unsure if there is some suitable transformation available.
>
> Thank you
> Jan
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> 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.
>



-- 
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40" S 49° 16' 22" O



More information about the R-help mailing list