# [R] Help with

Thu Oct 18 15:14:17 CEST 2012

```Hello,

It's much easier than you think, the first two columns of the input
matrix are the row and column numbers into the output matrix, therefore
those columns form an index matrix. Just see:

x <- scan(text="
1 1 3
1 2 54
1 3 11
1 4 17
2 1 5
2 4 78
2 5 20
")

mat <- matrix(x, ncol = 3, byrow=TRUE)

result <- matrix(0, max(mat[, 1]), max(mat[, 2]))
result[ mat[, 1:2] ] <- mat[, 3]

Easy, no?

Hope this helps,

Em 18-10-2012 13:44, Rui Esteves escreveu:
> Hi,
>
> I downloaded a dataset from UCI repositories named Bag of Words:
>
>
> The dataset is in a text file with the following structure:
> ---
>
> docID1 wordID1 count
> docID1 wordID2 count
> docID1 wordID3 count
> docID1 wordID4 count
> ...
> docID2 wordID2 count
> docID2 wordID5 count
> docID2 wordID6 count
> ---
>
> Where docIDx is an integer that identifies the document x; wordIDy is
> an integer that identifies the word y ; and count is an integer with
> the number of times that the wordIDy appears in the docIDx.
>
>
> Example:
>
> ---
>
> 1 1 3
> 1 2 54
> 1 3 11
> 1 4 17
> 2 1 5
> 2 4 78
> 2 5 20
> ---
>
> I would like to import the file into a matrix (not sparse) where:
>
> the wordIDy would correspond to the column [,y]
>
> the docIDx would correspond to the row [x,]
>
> the value in [x,y] would be the count of wordIDy in the docIDx
>
> So, for the previous example it would be like:
>
>
>      [,1][,2][,3][,4][,5]
>
> [1,]  3   54  11 17   0
>
> [2,]  5    0   0 78  20
>
>
> I don1t have a clue about how to do this.
>
>
> Thank you
>
> Rui
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help