I have the following numeric objects:
a1<-12
a2<-10
a3<-8
a4<-7
a5<-21
a6<-34
And I would love to have them as a column in a database. I can easily do:
df<-data.frame(value=c(a1,a2,a3,a4,a5,a6))
but I would love to be able to do it as a sequence. I've tryed a few things, like:
df<-data.frame(value=c(paste0("a",1:6)))
or
df<-value=c(get(paste0("a",1:6)))
But none of them put the values into the dataframe.
What is the right approach here?
Thanks!
CodePudding user response:
Since you are getting multiple objects, you should use mget()
instead.
df <- data.frame(value = unlist(mget(paste0("a", 1:6))))
value
a1 12
a2 10
a3 8
a4 7
a5 21
a6 34
CodePudding user response:
You can do it using map
and get
in r.
df <- data.frame(values = (map(paste0("a", 1:6), get) %>% unlist))
df
Output:
values
1 12
2 10
3 8
4 7
5 21
6 34
NOTE: You'll have to use dplyr
to use the pipeline.