简体   繁体   English

在R中,如何轻松地将许多向量组合成数据帧?

[英]In R, how to easily combine many vectors into a dataframe?

So, I know that I can use the data.frame command to combine multiple vectors into one dataframe, like so: 所以,我知道我可以使用data.frame命令将多个向量组合成一个数据帧,如下所示:

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

(I know, the variable names don't make much sense, why would I want name1, name20 and name38 in three different columns? Please ignore that: my actual variable names are different -- this is only for illustration purposes). (我知道,变量名称没有多大意义,为什么我要在三个不同的列中使用name1,name20和name38?请忽略它:我的实际变量名称是不同的 - 这仅用于说明目的)。

Problem is, I have about 40 of such vectors, and I have to combine many vectors in other parts of my code as well. 问题是,我有大约40个这样的向量,我必须在我的代码的其他部分组合许多向量。 So it would be convenient for me not to copy-paste a huge chunk of code every time. 因此,我不方便每次都复制粘贴大量代码。 So I thought of writing a for loop around this: 所以我想到围绕这个写一个for循环:

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 this doesn't work. 但这不起作用。 Is this because I should write "paste()" around some of this code, or is this simply a bad way to approach this problem? 这是因为我应该在这些代码中编写“paste()”,或者这只是解决这个问题的一种不好的方法吗? What is the correct way to loop through i and k and get a "newdata" dataframe as the final result with all the vectors as columns attached? 什么是循环通过i和k并获得“newdata”数据帧作为最终结果的正确方法,所有向量作为列附加?

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))

If all your individual vectors have a similar stem (eg: hgt ) and number (eg 1 ) arrangement, then you could do something like this: 如果你的所有单个载体都具有相似的词干(例如: hgt )和数字(例如1 )排列,那么你可以这样做:

# 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 limit the included vectors to the stem/number naming pattern to make sure you don't get any surprise additions to your dataframe. 这会将包含的向量限制为词干/数字命名模式,以确保您不会对数据帧进行任何意外添加。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM