Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save infotroph/00bc9cd61d22b5d2dcb565304a2c7545 to your computer and use it in GitHub Desktop.
Save infotroph/00bc9cd61d22b5d2dcb565304a2c7545 to your computer and use it in GitHub Desktop.
library(dplyr)
gather = tidyr::gather
df = data.frame(
one=c(1,NA,NA,3, NA),
two=c(NA,3,NA,4,NA),
three=c(NA,NA,2,NA,5),
x=rnorm(5))
result = (df
%>% add_rownames()
%>% gather(col, value, -x, -rowname)
%>% filter(!is.na(value))
%>% group_by(rowname)
%>% summarize(last(value), last(x)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment