Hi,
This line needs double brackets:
result_1 <- (result[[1]])
With single brackets you're keeping the list object. With double brackets
you're extracting just the object within the list.
Sarah
On Friday, July 4, 2014, Cheryl Johnson <johnson.cheryl625@gmail.com>
wrote:
> When I call matrices from a function, they are called but are not
> recognized as matrices. I use the code below. From the main function, when
> I use the command is.matrix(), the response is FALSE. Why are the matrices
> not recognized as matrices? Thanks in advance for any guidance.
>
> #This function creates matrices and returns them in a list.
>
> my_func <- function() {
>
> matrix_1 = matrix(c(1,2,3,4), ncol = 2, nrow = 2)
>
> matrix_2 = matrix(c(5,6,7,8), ncol = 2, nrow = 2)
>
> my_list <- list(matrix_1,matrix_2)
>
> return(my_list)
>
> }
>
> #This function calls the myfunc function.
>
> main_func <- function() {
>
> result <- myfunc()
>
> result_1 <- (result[1])
>
> print(result_1)
>
> print(is.matrix(a))
>
> result_2 <- (result[2])
>
> print(result_2)
>
> print(is.matrix(result_2))
>
> }
>
> main_func()
>
> > #This function creates matrices and returns them in a list.
>
> > my_func <- function() {
>
> + matrix_1 = matrix(c(1,2,3,4), ncol = 2, nrow = 2)
>
> + matrix_2 = matrix(c(5,6,7,8), ncol = 2, nrow = 2)
>
> + my_list <- list(matrix_1,matrix_2)
>
> + return(my_list)
>
> + }
>
> > #This function calls the myfunc function.
>
> > main_func <- function() {
>
> + result <- myfunc()
>
> + result_1 <- (result[1])
>
> + print(result_1)
>
> + print(is.matrix(a))
>
> + result_2 <- (result[2])
>
> + print(result_2)
>
> + print(is.matrix(result_2))
>
> + }
>
> > main_func()
>
> [[1]]
>
> [,1] [,2]
>
> [1,] 1 3
>
> [2,] 2 4
>
>
>
> [1] FALSE
>
> [[1]]
>
> [,1] [,2]
>
> [1,] 5 7
>
> [2,] 6 8
>
>
>
> [1] FALSE
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help@r-project.org <javascript:;> mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
--
Sarah Goslee
http://www.stringpage.com
http://www.sarahgoslee.com
http://www.functionaldiversity.org
[[alternative HTML version deleted]]