Search code examples
rdatatable

How to insert "US$" in R


I would like to insert US$ before the numbers in my datatable. For example, US$ 4. How can I do this?

library(DT)


Test <- structure(list(date2 = structure(c(18808, 18808, 18809, 18810
), class = "Date"), Category = c("FDE", "ABC", "FDE", "ABC"), 
coef1 = c(4, 1, 6, 1),coef2 = c(8, 3, 1, 6)), row.names = c(NA, 4L), class = "data.frame")

Test<-Test%>%mutate(Sum = rowSums(across(3:last_col()), na.rm = TRUE))

z<-datatable (Test, options = list(columnDefs = list(list(className = 'dt-center', targets = "_all")),
                                   paging =TRUE,searching = FALSE, pageLength =  10,dom = 'tip',scrollx=T),rownames = FALSE)

enter image description here


Solution

  • You may paste the text 'US $' for each numeric column.

    library(DT)
    library(dplyr)
    
    
    Test<-Test%>%mutate(Sum = rowSums(across(3:last_col()), na.rm = TRUE))
    Test <- Test %>% mutate(across(where(is.numeric), ~paste0('US $', .)))
    
    z<-datatable (Test, options = list(columnDefs = 
              list(list(className = 'dt-center', targets = "_all")),
                paging =TRUE,searching = FALSE, pageLength =  10,
              dom = 'tip',scrollx=T),rownames = FALSE)
    z
    

    enter image description here