Sample of 2 (made-up) example rows in df:
userid facultyid courseid schoolid
167 265 NA 1678
167 71111 301 NA
Suppose that I have a couple hundred duplicate userid like in the above example. However, the vast majority of userid have different values.
How can I combine rows with duplicate userid in such a way as to stick to the column values in the 1st (of the 2) row unless the first value is NA (in which case the NA will be repopulated with whatever value came from the second row)?
In essence, drawing from the above example, my ideal output would contain:
userid facultyid courseid schoolid
167 265 301 1678
Here's a different approach using
ddply
:Here's a simple one-liner from plyr. I wrote it a bit more generally than you asked:
Specifically answering the original question, if your data frame is named a, :
or use
dplyr
library: