0

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. Now, I would like to use the name of the data frame as suffixes in a paste inside my loop.

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?

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 (i.e. the name of the data frame in .df).

Can I do that?

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

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.

9
  • use for (nm in names(a)) { print(nm); .df = a[[nm]] } ? Commented Apr 29, 2012 at 2:42
  • @MartinMorgan, Thank you for answering my question, but I am looking for a way to subtract the names inside the loop when the data frames are stores in the .df element. Sorry for not being more specific in my initial question. Commented Apr 29, 2012 at 2:44
  • 1
    what does subtract the names mean? Can you add a snippet of the expected output? Commented Apr 29, 2012 at 3:07
  • @Chase, thank you for not giving up on my question. I've added a small update with a snippet of what I would like in the output. Please let me know if this is helpful. Commented Apr 29, 2012 at 3:18
  • 2
    Basically, the answer is ... the names are gone. The loop variable is an un-named dataframe by the time it is passed into the loop. As they say in New England when the bridge is washed out .. "seek alternate routes". Commented Apr 29, 2012 at 3:37

2 Answers 2

4

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

Sign up to request clarification or add additional context in comments.

Comments

0

Not a for loop but lapply but works similar to a for loop. Maybe this will help though I'm confused because I figured mrdwab's answer was it.

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

2 Comments

did you see my update? The thing is that I would like to stick to using the for ( .df in [list]) { loop-structure, but 'simply' subtract the name of the data frame inside the loop. Does my update help explain what I am looking for in any way? If not I will make a new update will my full problem. I'm not looking for help merging anything, I'm looking for help getting at the name of the element sores in .df during/inside the loop.
I do that here by passing the index to the lapply (or in your case the loop)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.