[R] Make a matrix from two lists

John Sorkin JSorkin at grecc.umaryland.edu
Fri Mar 29 18:54:17 CET 2013


Ben,
Thank you. When I used print I saw the matrix; the problem was as you indicated that I was using cat.
Thanks,
John

 
John David Sorkin M.D., Ph.D.
Chief, Biostatistics and Informatics
University of Maryland School of Medicine Division of Gerontology
Baltimore VA Medical Center
10 North Greene Street
GRECC (BT/18/GR)
Baltimore, MD 21201-1524
(Phone) 410-605-7119
(Fax) 410-605-7913 (Please call phone number above prior to faxing)>>> Ben Tupper <btupper at bigelow.org> 3/29/2013 1:49 PM >>>
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










Confidentiality Statement:
This email message, including any attachments, is for the sole use of the intended recipient(s) and may contain confidential and privileged information.  Any unauthorized use, disclosure or distribution is prohibited.  If you are not the intended recipient, please contact the sender by reply email and destroy all copies of the original message. 


More information about the R-help mailing list