First I will create the data to be used in this problem:
teste <- c("vetor_arte", "vetor_rio", "vetor_parque")
vetor_arte <- c(1, 3, 4, 11, 12, 13, 14, 16, 29, 30, 41)
vetor_rio <- c(6, 7, 8, 9, 10)
vetor_parque <- c(5, 15, 17, 27)
The function ls
lists the objects in the memory of R
. Here I’ll ask her to list all the objects they have vetor
in his some:
ls(pattern="vetor")
[1] "vetor_arte" "vetor_parque" "vetor_rio"
Then combine this result with the function lapply
, in order to create a list of the three desired vectors. Note that I have lost information about the name of the vectors, but this is not important in this case.
lista <- lapply(ls(pattern="vetor"), get)
lista
[[1]]
[1] 1 3 4 11 12 13 14 16 29 30 41
[[2]]
[1] 5 15 17 27
[[3]]
[1] 6 7 8 9 10
Finally, I create the object vetor_geral
when undoing the list created in the previous step:
vetor_geral <- unlist(lista)
vetor_geral
[1] 1 3 4 11 12 13 14 16 29 30 41 5 15 17 27 6 7 8 9 10
The disadvantage of this method is to put the vectors in alphabetical order. If the order vetor_arte
, vetor_rio
and vetor_parque
be important, my solution does not work satisfactorily.