简体   繁体   English

在循环中使用对象名称

[英]use object name in a loop

I'm merging some data frames that are stored in a list. 我正在合并一些存储在列表中的数据框。 For that purpose I'm using a for-loop with a .df command. 为此,我使用带有.df命令的for循环。 Now, I would like to use the name of the data frame as suffixes in a paste inside my loop. 现在,我想将数据框的名称用作循环中paste suffixes

Is there a way, using the for ( .df in [list]) { command that I can subtract the name of the data frame currently in .df inside the loop? 有没有办法使用for ( .df in [list]) {命令,我可以在.df内减去.df当前数据帧的名称?

Say I have this list with three data frames, 假设我有三个数据帧的清单,

a <- list(A = data.frame(a=runif(2), b=runif(2)), 
          B = data.frame(a=runif(2), b=runif(2)), 
          C = data.frame(a=runif(2), b=runif(2)))
a

$A
          a         b
1 0.2833226 0.6242624
2 0.1741420 0.1707722

$B
           a         b
1 0.55073381 0.6082305
2 0.08678421 0.5192457

$C
           a         b
1 0.02788030 0.1392156
2 0.02171247 0.7189846

Now, I would like to use this loop, 现在,我想使用这个循环,

for ( .df in a) {
 print(['command I do not know about'])
                }

and then have the [command I do not know about] print out A, B, C (ie the name of the data frame in .df ). 然后让[我不知道的命令]打印出A,B,C(即.df数据帧的.df )。

Can I do that? 我可以这样做吗?

Update 2012-04-28 20:11:58 PDT PDT更新2012-04-28 20:11:58

Here is a snipped of what I expect form my output using the simple loop from above, 这是我期望使用上面的简单循环从输出中得到的摘要,

for ( .df in a) {
 print(['command I do not know about'](a))
                }

[1] "A"
[1] "B"
[1] "C"

I could obtain this using, 我可以用

for (x in names(a)) {
    print(x)
    }

but due to the nature of what I am doing I would like to use the for ( .df in [list]) { command in my for-loop. 但是由于我的工作性质,我想在for ( .df in [list]) {使用for ( .df in [list]) {命令。

To extract both the name and the value you can't loop over the values. 要提取名称和值,您不能在值上循环。 You can loop over either the indices or the names: 您可以遍历索引或名称:

# Loop over indices (faster, more cumbersome)
ns <- names(a)
for(i in seq_along(a)) {
   v <- a[[i]]     # extract value
   n <- ns[[i]]    # extract name
   cat(n, ": \n")
   str(v)
}

# Loop over names (easy but slower)
for(n in names(a)) {
   v <- a[[n]]     # extract value
   cat(n, ": \n")
   str(v)
}

...looping over names and extracting values can be very slow for long vectors (it has n^2 time complexity). 对于长向量,循环名称和提取值可能非常慢(时间复杂度为n ^ 2)。

Not a for loop but lapply but works similar to a for loop. 不是for循环,而是适用于,但类似于for循环。 Maybe this will help though I'm confused because I figured mrdwab's answer was it. 也许这会有所帮助,尽管我很困惑,因为我认为mrdwab的答案就是这样。

FUN <-function(i){
    x <- a[[i]]
    data.frame(name=rep(names(a)[[i]], nrow(x)), x)
}

LIST <- lapply(seq_along(a), FUN)

do.call("rbind", LIST)

Yields: 产量:

  name          a         b
1    A 0.90139624 0.9739355
2    A 0.34311009 0.6621689
3    B 0.07585535 0.6010289
4    B 0.37292887 0.7260832
5    C 0.17814913 0.1433650
6    C 0.24586101 0.1741114

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

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