group by - roll-up record, max of each column, group_by R -
this seems simple, , have solution, it's kinda time consuming since have lot of columns. have looked @ other solutions, it's been different (aggregate 1 column, mutate columns etc). in sql select pat_id, max(x), max(y), max(z) table_name group pat_id
.
i have data set looks (but more columns):
dt <- data.frame( pat_id = c('p','p','p','a','a','a'), x = c(1,na,na, 1,na,na), y = c(na,2,na,na,1,na), z = c(na,na,1,na,na,0) )
so summarize , combine results:
results_x <-dt %>% group_by(pat_id ) %>% summarise(x = max(x, na.rm=true)) results_y <-dt %>% group_by(pat_id ) %>% summarise(y = max(y, na.rm=true)) results_z <-dt %>% group_by(pat_id ) %>% summarise(z = max(z, na.rm=true)) resulted <- left_join(results_x, results_y ) resulted <- left_join(resulted, results_z)
my output "roll-up" record max value each column per pat_id:
myresult <- data.frame( pat_id = c('p','a'), x = c(1,1), y = c(2,1), z = c(1,0) )
i'm sure there's better way this, how?
this can done summarize_all
in dplyr
. here go
library(dplyr) dt %>% group_by(pat_id) %>% summarize_all(max, na.rm=t) # pat_id x y z # <fctr> <dbl> <dbl> <dbl> # 1 1 1 0 # 2 p 1 2 1
Comments
Post a Comment