0

I am trying to combine two dataframes and with the following code. The problem I'm having is that for rbind it is literally just adding "Hello World" in each column of the last row. What I want to actually do is if i in the column name is "Hello World" add that row to newlist.

I realize more background maybe helpful, but I am really just trying to figure out this error. I am still learning R and any suggestions would be helpful.

MasterList <- function(group1, group) {
  first <- data.frame(group1)
  second <- data.frame(group)
  for (x in first$Name) {
    for (y in second$Name) {
      if (x != y) {
        newlist <- first[first$Name %in% second$Name, ] 
      }
    }
  }
  for (x in first$Name) {
    for (y in second$Name) {
      if (x == "Hello World") {
        NL <- rbind(newlist, i)     
      }
    }
    return(NL)
  }

output is

A header Another header
First row1
Second row2
Hello World Hello World

but I want the output to be

A header Another header
First row1
Second row2
Hello World row3
AlexisH
  • 33
  • 4
  • 2
    Your names are different. You're clearly defining `newlist` in one branch and `NL` in the other. After your `if` block is complete, you will have _either_ `newlist` _or_ `NL` defined, **never both**. This is "classic CS": if you reference a variable anywhere in your code, it must be defined somewhere before that point. What you define depends on how you intend to work with it. BTW: iteratively `rbind`ing things is a very bad habit, I strongly recommend you not do that. – r2evans Jul 26 '23 at 21:15
  • @r2evans Do you have any suggestions for appending data frames iteratively without rbind? Also, if you have any suggestions on my updated dilemma, I would really appreciate it. – AlexisH Jul 27 '23 at 20:34
  • (1) `lst <- list(); for (...) { ...; lst <- c(lst, list(newframe)); }; out <- do.call(rbind, lst)`. That last expression can be replaced with `dplyr::bind_rows(lst)` and `data.table::rbindlist(lst)`, both are more robust and flexible than the first. (2) No. The problem has been identified, you are conditionally creating variables then unconditionally referencing them. Even if you could get past the "not-yet-defined" part, it seems entirely too likely that your current `rbind(newlist, i)` would be appending a `newlist` from a previous portion of a loop. I can't follow the logic of what you need – r2evans Jul 27 '23 at 20:49
  • Making this more difficult, AlexisH, is that this problem is not reproducible: I don't know how you're calling it, what your _input_ data is, etc. It might help you to make it a self-contained reproducible question, please read through https://stackoverflow.com/q/5963269 , [mcve], and https://stackoverflow.com/tags/r/info on some discussions around this point. Bottom line: if you cannot start a fresh R instance and run all data/code in your question without errors like `object not found`, then it is not a reprex. – r2evans Jul 27 '23 at 20:51
  • @r2evans Essentially, I am trying to delete every row of the first dataframe where the column "name" is not concurrent with the second dataframe. That part works fine, then in the next block of for loops I am trying to say if the entry in the name column of the second data frame is Hello world add that to the new dataframe. I hope that makes sense. – AlexisH Jul 27 '23 at 20:58
  • You speak of "two frames" as if we know what they are. If they are 1-column frames, then fine. If they are 100-column frames and we care only about one column, then give us sample 1-column frames. Or use `mtcars` or `iris` or `ggplot2::diamonds` or `dplyr::starwars` to demonstrate a starting point. And please be specific about "concurrent": does that mean that if a name is present in the second frame but in a different row number, then it should be retained? – r2evans Jul 27 '23 at 21:08
  • Or are you saying "two frames" but really just "two columns in the same frame"? – r2evans Jul 27 '23 at 21:09
  • It may also be useful to explain how this question is different from your [previous question](https://stackoverflow.com/q/76665491/3358272), since that appears to be a very similar focus. – r2evans Jul 27 '23 at 21:12

0 Answers0