user3235
user3235

Reputation: 105

Refer to Index in Creating Data Frame R

I have variables v1,v2,etc and I want to create a dataframe.

I want to avoid doing:

df <-data.frame(v1,v2,...)

I would like to refer to the index in each of the variables and do something like:

for (i in 1:n){
df <-data.frame(v[i])
}

or do a max and min:

df <-data.frame(v1 to vn)

I just can't figure out what the proper syntax is.

Upvotes: 0

Views: 425

Answers (1)

flodel
flodel

Reputation: 89057

You can do:

as.data.frame(mget(paste0("v", 1:n)))

v1 <- 1:3
v2 <- 2:4
v3 <- 3:5

as.data.frame(mget(paste0("v", 1:3)))
#   v1 v2 v3
# 1  1  2  3
# 2  2  3  4
# 3  3  4  5

Upvotes: 1

Related Questions