In R, how easy is it to combine many vectors into a data frame?

So, I know that I can use the data.frame command to combine multiple vectors into one dataframe, for example:

my.data <- data.frame(name1, age1, name20, age20, name38, age38)

      

(I know variable names don't make a lot of sense, why would I want name1, name20, and name38 in three different columns? Please ignore this: my actual variable names are different - this is just for illustration.)

The problem is that I have about 40 such vectors, and I have to combine many vectors in other parts of my code as well. Therefore, it would be convenient for me not to copy-paste a huge piece of code every time. So I thought about writing a for loop:

for (i in c("name", "age", "hgt"))
{
    for (k in c(1,20,38))
    {
    my.data$i,as.character(k) <- data.frame(get(paste(i,as.character(k),sep="")))
    }
}

      

But it doesn't work. Is it because I have to write "paste ()" around some of this code, or is it just a bad way to approach this problem? What is the correct way to loop through i and k and end up with a "newdata" framework with all the vectors as columns?

+1


source to share


2 answers


Are you trying to achieve something like the following, perhaps?



name1 <- letters[1:10]
age1 <- 1:10
name20 <- letters[11:20]
age20 <- 11:20
name38 <- LETTERS[1:10]
age38 <- 21:30

paste.pattern <- paste(rep(c("name", "age"), times = 3), 
                       rep(c(1, 20, 38), each = 2), sep = "")

newdata <- data.frame(sapply(paste.pattern, get))

      

+2


source


If all of your individual vectors have a similar structure (ex:) hgt

and number (ex:) 1

, then you can do something like this:

# test data
name1 <- letters[1:10]
age1 <- 1:10
name20 <- letters[11:20]
age20 <- 11:20
name38 <- LETTERS[1:10]
age38 <- 21:30

# group them up in a dataframe looking for "age" OR (|) "name" as the stem
data.frame(sapply(ls(pattern="^age[0-9]+$|^name[0-9]+$"),get))

# result:
   age1 age20 age38 name1 name20 name38
1     1    11    21     a      k      A
2     2    12    22     b      l      B
3     3    13    23     c      m      C
4     4    14    24     d      n      D
5     5    15    25     e      o      E
6     6    16    26     f      p      F
7     7    17    27     g      q      G
8     8    18    28     h      r      H
9     9    19    29     i      s      I
10   10    20    30     j      t      J

      



This will restrict the included vectors to the stem / number naming pattern to make sure you don't get any surprises for your frame.

+1


source







All Articles