I have a dataframe that has two rows:
| code | name | v1 | v2 | v3 | v4 |
|------|-------|----|----|----|----|
| 345 | Yemen | NA | 2 | 3 | NA |
| 346 | Yemen | 4 | NA | NA | 5 |
Is there an easy way to merge these two rows?
What if I rename “345” in “346”, would that make things easier?
You can use
aggregate. Assuming that you want to merge rows with identical values in columnname:This is like the SQL
SELECT name, min(v1) GROUP BY name. Theminfunction is arbitrary, you could also usemaxormean, all of them return the non-NA value from an NA and a non-NA value ifna.rm = TRUE.(An SQL-like
coalesce()function would sound better if existed in R.)However, you should check first if all non-NA values for a given
nameis identical. For example, run theaggregateboth withminandmaxand compare, or run it withrange.Finally, if you have many more variables than just v1-4, you could use
DF[,!(names(DF) %in% c("code","name"))]to define the columns.