Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

In R, how to quickly create dataframe from a series of variables with indexed name?

I want to create a data frame from a series of vectors that I have named like d*i*. I attempted to do this using the paste function, but it just built a data frame with a single column with values "d1, d2, d3…" I want to put the vectors associated with those variable names in. How do I do this?

d1 <- c(6.3,8.3,6.6,0,8.4,8.6)
d2 <- c(8.2,8.7,8.6,7.9,7.1,7.6)
d3 <- c(7.1,6.4,6.6,8,7.5,10.3)
d4 <- c(8,7.7,7.3,0,9.4,6.4)
d5 <- c(8.5,6.8,0,0,7.3,9.7)
d6 <- c(5.9,7.5,6.5,0,9.8,7.8)
d7 <- c(7.6,5.3,6.7,0,6.6,7.4)
d8 <- c(9.5,5.6,8.8,0,8.6,8.3)
d9 <- c(8.5,7.4,0,0,9.2,8.6)
d10 <- c(7.8,6.9,8.6,6.7,6.8,6.1)

mydata <- data.frame(paste("d",1:10))

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You can do this:

my_data = as.data.frame(mget(paste0("d", 1:10)))

Though you should probably think about not creating sequentially named variables in the first place–maybe put them in a data frame or a list directly.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading