Search code examples
rdataframemedian

Write values into brackets for each row


Currently I have a data frame like:

       case_med   case_min  case_max
IL8    4.022518 -0.8826681 11.482423
VEGFA 10.118825  5.8325935 11.865825
CD8A  12.232512  3.9384582 15.873059
MCP3   1.483711 -0.4865190  4.882046
GDNF   1.773944 -0.9836022  4.774641
CDCP1  2.567742  0.3207944  6.024392

I want to transform to the format like median(min,max) for each row, something like:

        case_med(case_min,case_max)
IL8    4.022518(-0.8826681,11.482423)
VEGFA 10.118825(5.8325935, 11.865825)
CD8A  12.232512(3.9384582,15.873059)
MCP3   1.483711(-0.4865190,4.882046)
GDNF   1.773944 (-0.9836022,4.774641)
CDCP1  2.567742(0.3207944,6.024392)

After transform, the new data frame should only have one column interprets "case_med(case_min,case_max)". How can I combine value from other two columns into the brackets and store these in one column? Thanks in advance!


Solution

  • You can paste() the columns together (paste0() to be precise):

    data.frame(case_med.min_max = paste0(df$case_med, "(", df$case_min, ",", df$case_max, ")"),
               row.names = rownames(df))
    
                        case_med.min_max
    IL8   4.022518(-0.8826681,11.482423)
    VEGFA 10.118825(5.8325935,11.865825)
    CD8A  12.232512(3.9384582,15.873059)
    MCP3    1.483711(-0.486519,4.882046)
    GDNF   1.773944(-0.9836022,4.774641)
    CDCP1   2.567742(0.3207944,6.024392)
    

    Data:

    df <- read.table(header = TRUE, text = "       case_med   case_min  case_max
    IL8    4.022518 -0.8826681 11.482423
    VEGFA 10.118825  5.8325935 11.865825
    CD8A  12.232512  3.9384582 15.873059
    MCP3   1.483711 -0.4865190  4.882046
    GDNF   1.773944 -0.9836022  4.774641
    CDCP1  2.567742  0.3207944  6.024392")