0

I have a set of vector (over 2k) that each contain over 13k entries. I need to loop over each of them to place the data in a txt files for future uses.

So I thought I would create a list, containing the name of all the vector:

vector_list <- list()
for (i in 1:Number) {
  vec <- paste("vec", i, sep="")
  assign(vec, data[i,1])
  vector_list[i] <- vec
}

Only problem is that when I access the value of vector_list[-], I get the correct name, but it's in between "" so I cannot use it to evaluate the value of the vector whose name is between the "". when I write

  TEST <- vector_list[i]

I do not get the value contained in the veci (whose name is stored in the vector_list[i]) but I get "veci"... Any help about this?

I also tryed the command get which does not seems to work. For example, if I type get(vector_list[1]) I get Error in get(vector_list[1]) : invalid first argument. Similar if I use a<-vector_list[1] then get(a). Perhaps I am the one misusing the get fonction..?

John
  • 61
  • 5
  • 1
    Use `get`, or even better use a `list` instead of sequentially named variables. – Gregor Thomas Feb 27 '17 at 19:53
  • How did you wind up with all these vectors with sequential names in the first place? This is a bad-code-smell in R. There are likely more functional ways of going this stuff in R but your question leaves out much of the context that would be helpful to suggest alternatives. – MrFlick Feb 27 '17 at 19:55
  • The get command does not seems to work. – John Feb 27 '17 at 20:09
  • Each vector represent a time step in a FE analysis. – John Feb 27 '17 at 20:10
  • Well, your example isn't reproducible, so it still looks like a duplicate. My best guess for your bug is that you aren't indexing your list correctly, `vector_list[1]` is a one-element list containing "vec1", but you need to use `vector_list[[1]]` to just get `"vec1"` as a character. (Or use a regular vector, not a list.) The improvement would be to just use a `list` of values not a list of names of variables, i.e., make your entire `for` loop body `vector_list[[i]] <- data[i,1]`. (Or skip the for loop and just do `vector_list = split(data[, 1], 1:nrow(data))`.) – Gregor Thomas Feb 27 '17 at 21:18
  • If you need more explicit help, please [make your example reproducible](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – Gregor Thomas Feb 27 '17 at 21:18

0 Answers0