[R] Make a matrix from two lists
Ben Tupper
btupper at bigelow.org
Fri Mar 29 18:49:33 CET 2013
Hi,
On Mar 29, 2013, at 1:29 PM, John Sorkin wrote:
> I am trying to write a function that makes a matrix out of two lists. As you will see run running the function below, the result is NOT a matrix but rather a list. Can anyone tell me what I am doing wrong?
> Thanks,
> John
>
> test <- function(one,two) {
> cat("List One=",one,"\n")
> cat("List Two=",two,"\n")
> cat("Should be a matrix, but is not",matrix(cbind(one,two)),"\n")
> }
> x <- c(1,2,3)
> y <- c(11,22,33)
> test(x,y)
>
x <- c(1,2,3)
y <- c(11,22,33)
z <- cbind(x,y)
is.matrix(z)
[1] TRUE
As you can see above, I don't get the same result.
I not sure if you want to see the matrix printed in matrix form when you have it in test(). If that were the case then you'll want to avoid cat() and use print() instead.
test <- function(one,two) {
cat("List One=",one,"\n")
cat("List Two=",two,"\n")
z <- cbind(one, two)
print(z)
}
test(x,y)
List One= 1 2 3
List Two= 11 22 33
one two
[1,] 1 11
[2,] 2 22
[3,] 3 33
Cheers,
Ben
Ben Tupper
Bigelow Laboratory for Ocean Sciences
60 Bigelow Drive, P.O. Box 380
East Boothbay, Maine 04544
http://www.bigelow.org
More information about the R-help
mailing list