3

I've got a simple problem that I haven't been able to solve despite the many similar posts, because I'm a bit of a knucklehead in R, and I'm not getting whatever it is I should be getting. I have two sets of files,

All.Files <- objects(pattern="constant.country[0-9]{4}")
all.files <- objects(pattern="constant[0-9]{4}")

that I wish to merge

mergefun <- function(X1, Y1) {
    merge(X1, Y1, by = "id")
} 

and then save each iteration of merge into a new dataframe

for (i in All.Files) {
    a <- get(i)
      { for (j in all.files)
         b <- get(j)
         d <- dataframe(mergefun(a, b))
         newname <- paste("C", substr(j, 9, 12), sep="")
         names(d) <- c("Id", "Country", "logGDP", "GRI", GRI.group", "Year")
         assign(newname,d)

       }  
    }

While I am sure there is more elegant code out there, this code does what I want it to. The problem is that it is only saving the final iteration of merge, so that instead of getting 1:43 dataframes, I only get the 43rd. I know I am failing to index properly within the for loop, but I have struggled for hours to understand my mistake and have failed.

I am sorry I have not included a reproducible example, but was hopeful that because my code actually worked, someone would be able to instantly see what I am missing that would allow all 43 iterations to get outputted via assign. I am also aware that mapply would likely be a better solution, but I was unable to get any traction with it despite several hours of trying!

A most humble (and humbled by my own ignoRance) thank you.

8
  • Is that the EXACT code you're using? If so, you have an extra quote after GRI.group. Commented Mar 14, 2014 at 23:09
  • funny--that's the only line of code I changed. I took out a few column headers to simplify it and messed up the quote when doing so. So no, there are no issues with improper quotes when I run it. I get no errors at all; my problem is that I only get iteration 43 outputted as a df. Commented Mar 14, 2014 at 23:16
  • Do you have an issue where the substring of j you are pulling out is always the same? Commented Mar 14, 2014 at 23:30
  • Just took another look at your code. Your brackets are messed up. Commented Mar 14, 2014 at 23:35
  • In what sense are they messed up? Commented Mar 14, 2014 at 23:40

1 Answer 1

4
for (i in All.Files) {
a <- get(i)
   for (j in all.files) {
     b <- get(j)
     d <- dataframe(mergefun(a, b))
     newname <- paste("C", substr(j, 9, 12), sep="")
     names(d) <- c("Id", "Country", "logGDP", "GRI", "GRI.group", "Year")
     assign(newname,d)

   }  
}

I fixed your bracketing. It should also be clear that since your newname only relies on j that it will be the case that your new variables will only be created on the LAST iteration of the outer for loop. Thus, you end up with only length(all.files) variables.

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

8 Comments

Thank you! What you did works. It took me a few minutes to sort out why having the bracket enclose the j for-loop caused the problem, but I see it now. Thank you for answering such a simple question--taught me s/thing I'll use in all my new code construction.
No problem. Happy to help, especially since you formed your question well. Do heed the fact that the vars you are creating only rely on j! I think your code won't output quite what you want as is!
Rats. You are right. Back to the drawing board. In original code it only gave me iteration 43 but it was the correct output. Now I get all 43 outputs and they are wrong. One step at a time, eh?
Just have you newname variable rely on an identifier from i. This will make them unique.
Even if I use i in the newname it still only uses the last iteration of j in each merge (but it uses i[1:43].
|

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.