Giovanni Corradini
Giovanni Corradini

Reputation: 21

what does rbind.fill.matrix really do?

I have this code and can't understand how rbind.fill.matrix is used. dtmat is a matrix with the documents on rows and words on columns.

word <- do.call(rbind.fill.matrix,lapply(1:ncol(dtmat), function(i) {
    t(rep(1:length(dtmat[,i]), dtmat[,i]))
}))

I read the description of the function and says that binds matrices but cannot understand which ones and fills with NA missing columns.

Upvotes: 2

Views: 611

Answers (1)

Dinesh.hmn
Dinesh.hmn

Reputation: 711

From what I understand, the function replaces columns that dont bind with NA. Lets say I have 2 matrices A with two columns col1 and col2, B with three columns col1, col2 and colA. Since I want to bind all both these matrices, but rbind only binds matrices with equal number of columns and same column names, rbind.fill.matrix binds the columns but adds NA to all values that should be in both the matrices that are not. The code below will explain it more clearly.

a <- matrix(c(1,1,2,2), nrow = 2, byrow = T)
> a
      [,1] [,2]
[1,]    1    1
[2,]    2    2
> 
> b <- matrix(c(1,1,1,2,2,2,3,3,3), nrow = 3, byrow = T)
> b
      [,1] [,2] [,3]
[1,]    1    1    1
[2,]    2    2    2
[3,]    3    3    3
> 
> library(plyr)
> r <- rbind.fill.matrix(a,b)
> r
     1 2  3
[1,] 1 1 NA
[2,] 2 2 NA
[3,] 1 1  1
[4,] 2 2  2
[5,] 3 3  3
> 
> 

The documentation also mentions about column names, which I think you can also understand from the example.

Upvotes: 2

Related Questions