I’m using cbind to bring together a matrix of string and numerics, and it is returning all of the columns of numerics (except the first one) as NULL. Here is sample code:
nums <- matrix(data=c(1,2,3,4),ncol=2)
strs <- list()
strs[1] <- 'row1'
strs[2] <- 'row2'
result <- cbind(strs,nums)
> result
strs
[1,] "row1" 1 NULL
[2,] "row2" 2 NULL
I’m obviously overlooking something simple. Thanks for the help,
In this case, I think you’re going about the problem in slightly the wrong way.
cbindreturns a matrix. One of the limitations of matrices is that all elements must be of the same type. This means yourc(1, 2, 3, 4)will, when coerced into a matrix, become the highest level data type present (in this case character fromstrs) and become the equivalent ofc("1", "2", "3", "4").I’m going to assume that you have a list
strswith 2 elements and that what you actually want is a data.frame (which is able to hold different types).In either case, let’s convert
strsinto a character first:Note that this is the same as if you’d created it via
c("row1", "row2").Next, we’ll create a data.frame:
This is essentially the same solution that @thelatemail provided.