0

I am just trying to do a basic thing but I can't seem to figure out what the problem is and I can't find answers here that are to problems exactly like mine. If anyone already knows of an answer to this elsewhere, feel free to link that.

I have a simulation that generates a vector, and I have set up my simulation such that it grabs the generate vector and makes it an element of another vector. After I run the simulation multiple times, I would like to make the vector of vectors into a matrix, but it the console output is always this:

 >   agx1     
[1,] Numeric,7
[2,] Numeric,7

My simulation pretty much does the following:

agnx1 = c()

#some stuff happens

agnx1[i] = x1

#iteration number two takes place

agnx1[i+1] = x1

#etc..

#Now say I have

agx1[1] = c(0.796399, 0.865736, 0.885808, 0.896138, 0.896138, 0.850385, NA)

#and

agx1[2] = c(0.796399, 0.856540, 0.881432, 0.900808, 0.900808, 0.857664, NA)

#and therefore, agx1 is a vector of vectors. But whenever I try something like..

cagx1 = cbind(agx1[1:2])

#or

cagx1 = as.matrix(agx1)

# I just get:   

 [,1]     
[1,] Numeric,7
[2,] Numeric,7

Any suggestions would be helpful.

Nate River
  • 51
  • 6

2 Answers2

2

It's hard to tell without seeing all of the data, but perhaps agx1 is a list. Try using do.call.

do.call(cbind, agx1)

Edit

Base R cbind doesn't have functionality to work on a list. Consider this:

cbind(agx1[[1]],agx1[[2]])

That works because you have unlisted the first and second elements and passed them as vectors to cbind.

You get around this problem using do.call. help(do.call) says:

Description

do.call constructs and executes a function call from a name or a function and a list of arguments to be passed to it.

Thus, do.call helps you call cbind(agx1[[1]], agx1[[2]], ... and so on until the end of the list by constructing the cbind function call from the list of agx1 arguments.

Ian Campbell
  • 23,484
  • 14
  • 36
  • 57
0

Assuming agx1 is something like this :

agx1 <- list(c(0.796399, 0.865736, 0.885808, 0.896138, 0.896138, 0.850385, NA),
             c(0.796399, 0.856540, 0.881432, 0.900808, 0.900808, 0.857664, NA))

You could use dplyr::bind_cols

dplyr::bind_cols(agx1)

#     V1     V2
#   <dbl>  <dbl>
#1  0.796  0.796
#2  0.866  0.857
#3  0.886  0.881
#4  0.896  0.901
#5  0.896  0.901
#6  0.850  0.858
#7     NA     NA    
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213