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?