R data table group by and unique values

30 views Asked by At

given the start, can you modify the code of the end_current to get instead the end_goal? basically other than the minimum I need all the unique values

start <- data.table(team=c('a', 'a', 'b', 'b', 'b', 'c', 'c'),
                    pts=c(5, 8, 5, 5, 5, 9, 10))




end_goal <- data.table(team=c('a','b','c'),
                  min_pts=c(5, 5, 9),
                  unique_pts=c("5-8","5","9-10"))

end_current <- start[, .(
                  min_pts = min(pts)
                  # unique_pts???
                  ),
              by = .(team)]

1

There are 1 answers

0
s_baldur On BEST ANSWER
start[, .(min_pts = min(pts), unique_pts = paste(unique(pts), collapse="-")), 
      by = team]

#      team min_pts unique_pts
#    <char>   <num>     <char>
# 1:      a       5        5-8
# 2:      b       5          5
# 3:      c       9       9-10