tcash21
tcash21

Reputation: 4995

Compare pairs of values with same ID in a data.frame and return boolean for each row

I have a data.frame of pairs of values with a common ID. I simply want a TRUE/FALSE for each row based on whether its value is greater than its paired value.

Here's the data:

d<-structure(list(id = c(400585859L, 400585859L, 400585862L, 400585862L,400585863L, 400585863L, 400585867L, 400585867L, 400585868L, 400585868L), pts = c(69L, 70L, 77L, 70L, 76L, 69L, 89L, 76L, 73L, 75L)), .Names = c("id","pts"), row.names = c(NA, -10L), class = "data.frame")

If I use ddply I end up with only 5 rows instead of 10:

ddply(d, .(id), summarize, pts[1] > pts[2])

If my data looks like this:

      id pts
  400585859  69
  400585859  70
  400585862  77
  400585862  70
  400585863  76
  400585863  69
  400585867  89
  400585867  76
  400585868  73
  400585868  75

I'd like:

     id pts
      400585859  69 FALSE
      400585859  70 TRUE
      400585862  77 TRUE
      400585862  70 FALSE
      400585863  76 TRUE
      400585863  69 FALSE
      400585867  89 TRUE
      400585867  76 FALSE
      400585868  73 FALSE
      400585868  75 TRUE

Upvotes: 2

Views: 1177

Answers (2)

Steven Beaupr&#233;
Steven Beaupr&#233;

Reputation: 21621

Here is one using dplyr:

library(dplyr)
d %>% group_by(id) %>% mutate(status = pts > min(pts))

Upvotes: 2

Ramnath
Ramnath

Reputation: 55695

Here is one solution

ddply(d, .(id), transform, status = pts > min(pts))

Upvotes: 3

Related Questions