I am trying to retrieve the index of a newly-added row, added via a for loop.

Starting from the beginning, I have a list of matrices of p-values, each with a variable number of rows and columns. This is because not all groups have an adequate number of treated individuals to run t-tests. The following is what prints to the console when I access this sample list:

```
$Group1
Normal Treatment 1 Treatment 2
Treatment 1 1 NA NA
Treatment 2 1 1 NA
Treatment 3 1 1 1
$Group2
Normal Treatment 2
Treatment 2 1 NA
Treatment 4 1 1
```

I would like every group to have the same number of rows and columns, in the correct order, with the missing values just filled in with NAs. This is a sample of what I would like:

```
$Group1
Normal Treatment 1 Treatment 2 Treatment 3
Treatment 1 1 NA NA NA
Treatment 2 1 1 NA NA
Treatment 3 1 1 1 NA
Treatment 4 NA NA NA NA
$Group2
Normal Treatment 1 Treatment 2 Treatment 3
Treatment 1 NA NA NA NA
Treatment 2 1 NA NA NA
Treatment 3 NA NA NA NA
Treatment 4 1 1 NA NA
```

Here is the code I have so far:

```
fix.results.row <- function(x, factors) {
results.matrix <- x
num <- 1
for (i in factors){
if (!i %in% rownames(results.matrix)) {
results.matrix <- rbind(results.matrix, NA)
rownames(results.matrix)[num] <- i
}
num <- num + 1
}
rownames(results.matrix) <- results.matrix[rownames(factors),,drop=FALSE]
return(results.matrix)
}
```

In the function above, x would be my list of matrices, and factors would be a list of all the factors in the order I want them. I have a similar function for adding columns.

My problem, as I see it, is in Group 2. If it sees that I'm missing Treatment 1, it will replace the rowname Treatment 2 with the rowname Treatment 1, so the data for Treatment 2 is now mislabeled Treatment 1. Then it reorders the variables the way I want them, but the data are already mislabeled!

If I could access the index of the newly-added row, which changes from group to group, then I could just change that specific row name. Any suggestions? Please let me know if there's any more information I need to provide. I tried to cover everything but I'm not sure if there's anything else you all need.