0

This is probably easy, but in a grouped data frame, I'm trying to find the difference in diff.column between the last row and the row where var.col is B The condition only appears once within each group. I'd like to make that difference a new variable using summarize from dplyr.

my.data<-data.frame(diff.col=seq(1:10),var.col=c(rep('A',5),'B',rep('A',4)))

I'd like to keep this in dplyr and I know how to code it except for selecting diff.col where var.col==B.

my.data%>%summarize(new.var=last(diff.col)-????) 
tlyons253
  • 83
  • 8
  • 2
    You can do: `my.data %>% summarize(difference = last(diff.col) - diff.col[var.col == "B"])` -- are you saying you don't want this solution? – George Wood Jun 29 '17 at 20:45
  • Perfect! I knew it was simple, i was just bashing my head against a wall trying to use filter. Thanks! – tlyons253 Jun 29 '17 at 20:47

0 Answers0