I have a dataframe df which has 5 rows and 6 columns.
df <- data.frame(
Hits = c("Hit1", "Hit2", "Hit3", "Hit4", "Hit5"),
category1 = c("a1", "", "b1", "a1", "c1"),
category2 = c("", "", "", "", "a2"),
category3 = c("a3", "", "b3", "", "a3"),
category4 = c("", "", "", "", ""),
category5 = c("", "", "a5", "b5", ""),
stringsAsFactors = FALSE)
From each of the columns category1 to category5, I need to retain only the elements which appear at the topmost position i.e.
and finally, drop the rows having no elements in these five columns, i.e.
How do I achieve this in the simplest possible way in R?


