I have a vector of sample IDs that are required to be in my dataframe (otherwise the function I am applying to them doesn't work) but are missing (called missing
).
For each of the elements in missing
, I want to add a row to the end of my dataframe where I include the ID but the rest of the data (for all the other columns) in the row is all NAs.
What I am currently trying, based on some other Stack Overflow posts I saw that talk only about adding empty rows, is as follows:
for (element in missing) {
df[nrow(df)+1,] <- NA
df[nrow(df),1] <- element
}
Is there a simpler and faster way to do this, since it takes some time for even 1000 missing elements, whereas I might later have to deal with a lot more.