0

I'm trying to work on a dataframe on R about banks for my thesis! In particular I have to select some rows from my dataset.

  • First I check if there are 2 equal name in the column "company name"
  • Second I check which is the "consolidation code" in the column
  • Lastly, depending on the consolidation code, I try to assign the row of my dataset to a new dataset.

The last point is the one where I'm having problems.. I think I'm following all the instructions from how to assign a row to a dataset, but still cannot find a solution and I hope you can help me!

data <- data.frame()
for (i in 1:nrow(total)) {
  for (j in 2:nrow(total)){
    if(total$Company_name[i]==total$Company_name[j] & total$Consolidation_code[i] == "U2")
       {data <- total[i,]}
    else if (total$Company_name[i]==total$Company_name[j] & total$Consolidation_code[i] == "C2")
       {data <- total[i,]}
    else if (total$Company_name[i]==total$Company_name[j] & total$Consolidation_code[i] == "C1")
       {data <- total[i,]}
    else if (total$Company_name[i]==total$Company_name[j] & total$Consolidation_code[i] == "U1")
       {data <- total[i,]}
                       }
                     }

This is the code I wrote.. please let me know if you can spot my mistake and let me know if you know an easier way to code my problem. Thanks in advance.

UPDATE now this code works, but it only assign 1 row to the new dataset.. can you spot the problem?

  • 1
    Could you please share your data using `dput(data)`? So we can help you better by reproducing your problem. – Quinten Apr 02 '22 at 18:50
  • Sounds like a join or merge with an additional condition for me. Check out https://stackoverflow.com/questions/1299871/how-to-join-merge-data-frames-inner-outer-left-right – Martin Gal Apr 02 '22 at 19:07
  • @Quinten the dataframe is an excel file that I dowloaded from bankfocus, do you think I can attach somehow it? – Martina Noli Maio Apr 02 '22 at 19:08
  • @MartinGal thank you for your answer, but I don't have to merge 2 datasets.. I have to create a new one with the rows of the old dataset that verify the if condition – Martina Noli Maio Apr 02 '22 at 19:11
  • Please share a reproducible example. I still believe a `join` or `merge` could be of use. – Martin Gal Apr 02 '22 at 19:43
  • @MartinGal I found out that I just needed to add 'else if' condition since I have so many else.. problem: it seem that it only execute the for loop once.. can you imagine why? and as already said, the dataset I'm using I did not find it on internet, but I downloaded from bankscope.. so I don't have any idea about how to show it – Martina Noli Maio Apr 02 '22 at 19:52
  • You should replace `for (j in nrow(total)) ` with `for(j in 1:nrow(total))`. Else it will only be executed for one value of `j`. The same for `i`. – Martin Gal Apr 02 '22 at 20:48
  • it only adds 1 row :( @MartinGal.. but really thanks so much for trying to help!! – Martina Noli Maio Apr 02 '22 at 21:02
  • You could try `data <- rbind(data, total[i,]) ` instead of `data <- total[i,]`. The latter overwrites `data` with each iteration, so only the last call will be saved. – Martin Gal Apr 02 '22 at 21:28
  • OMG!!! you made it!!! it worked!! I only had to remove duplicates (for sure I can improve the code.. but at least now I have the dataframe on which I can work)... THANKS @MartinGal – Martina Noli Maio Apr 02 '22 at 23:05
  • You're welcome. Next time please try to make a great reproducible example. You're more likely to get a good and fast answer for your problem. :) – Martin Gal Apr 03 '22 at 00:22

0 Answers0