0

I have a categorical covariate data summary as below:

 df <- 
 STUDY   COV   FLAG   Freq
 1       SEX   1       12
 1       SEX   2       15
 2       RACE  1       5
 2       RACE  2       10
 2       RACE  3       15

I want to make a summary by STUDY and COV to facilitate generating an rtf table. This the output that i would like to have is this.

 dfout <-
 STUDY   COV   RATIO
 1       SEX   12:15
 2       RACE  5:10:15

I tried using this but i know it is getting no where

  library(dplyr)
  dfout<- df%>% 
    group_by(STUDY,COV) %>% 
    summarise(RATIO=paste(Freq,":",lag(Freq)))

i am using Rstudio.

Amer
  • 2,131
  • 3
  • 23
  • 38
  • 1
    `df%>% group_by(STUDY,COV) %>% summarise(RATIO = paste(Freq, collapse = ":"))` will do. It seems that you are a bit thinking too much, perhaps? – jazzurro Jan 18 '18 at 04:08
  • 1
    thanks, that worked! – Amer Jan 18 '18 at 04:13
  • Perhaps your question is very similar to [this post](https://stackoverflow.com/questions/15933958/collapse-concatenate-aggregate-a-column-to-a-single-comma-separated-string-w). – jazzurro Jan 18 '18 at 04:14

1 Answers1

1

No need for lag, paste with collapse parameter should do:

df%>% 
    group_by(STUDY, COV) %>% 
    summarise(RATIO = paste(Freq, collapse=":"))

# A tibble: 2 x 3
# Groups: STUDY [?]
#  STUDY COV    RATIO  
#  <int> <fctr> <chr>  
#1     1 SEX    12:15  
#2     2 RACE   5:10:15
Psidom
  • 209,562
  • 33
  • 339
  • 356