I create a function that allows to join data frames usingrbind()
The function is as follows (you can run it since it is generated with random data)
juntador<-function(x){
for (i in 1:x) {
assign(paste0("data_",i), data.frame(var1=sample(1:10), var2=sample(1:10)))
}
lista<-lapply(ls(pattern = "^data"), get)
junte<-do.call(rbind, lista)
return(junte)
}
Then, the function takes a number as an argument, and based on this, data frames are created, and then they are joined.
For example, if I consider 3. I should join the 3 dataframes that I just created.
juntador(3)
# NULL
But it throws me NULL
, I presume this is because in the execution of the lapply
it was not saved in the environment. So I put this on each of the lines.
juntador<-function(x){
for (i in 1:x) {
assign(paste0("data_",i), data.frame(var1=sample(1:10), var2=sample(1:10)),
envir = globalenv())
}
lista<-lapply(ls(pattern = "^data"), function(x){get(x, envir = globalenv())})
junte<-do.call(rbind, lista, envir = globalenv())
return(junte)
}
But I still can't get what I want. He keeps throwing me NULL
.
For what is this? Where do I have to correct?