how to aggregate same name in the same column which is not numeric

how to aggregate the same province/statename .I have tried couple of times by passing the given below line of code but it threw the error ,I think not having a numeric column caused to this error,if i am wrong ,please correct me and anyone could guide me through how to rectify this issue.
aggregate(. ~ total$province/state,data=total,fun=sum)
aggregate(total ,by=list(total$province/state,sum,na.rm=true)
I have tried switching the parameters in case of some kind of basic syntax error but that's not the issue ,I guess.

library(shiny)
library(plotly)
## Loading required package: ggplot2
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
library(DT)
## 
## Attaching package: 'DT'
## The following objects are masked from 'package:shiny':
## 
##     dataTableOutput, renderDataTable
COL = c("#293a80","#39375b","#6915cf","#4b8e8d","#d55252","#293462","#940a37","#f54291","#f0134d","#b22222","#3c4245","#5d1451","#3c3d47","#1a3e59","#233714","#3c6f9c","#1089ff","#394a6d","#3c70a4","#602080","#02383c","#5d5d5d","#543864","#042f4b","#293462","#4b8e8d","#d55252","#293462","#940a37","#f54291","#f0134d","#b22222")
  #---
# deaths<-read.csv("C:/Users/dell/Downloads/archive (2)/time_series_covid_19_deaths.csv")
# confirmed<-read.csv("C:/Users/dell/Downloads/archive (2)/time_series_covid_19_confirmed.csv")
# recovered<-read.csv("C:/Users/dell/Downloads/archive (2)/time_series_covid_19_recovered.csv")
#total<-read.csv("C:/Users/dell/Downloads/archive (2)/test.csv")
#aggregate_value<-aggregate(total,by=list(total$Province.State),FUN = sum,na.rm=TRUE)
total<-data.frame(
  stringsAsFactors = FALSE,
       check.names = FALSE,
                      SNo = c(1L,2L,3L,4L,
                              5L,6L,7L,8L,9L,10L,11L,12L,13L,14L,15L,
                              16L,17L,18L,19L,20L,21L,22L,23L,24L,25L,
                              26L,27L,28L,29L,30L,31L,32L,33L,34L,35L,
                              36L,37L,38L,39L,40L,41L,42L,43L,44L,45L,
                              46L,47L,48L),
          ObservationDate = c("1/22/2020",
                              "1/22/2020","1/22/2020","1/22/2020","1/22/2020",
                              "1/22/2020","1/22/2020","1/22/2020","1/22/2020",
                              "1/22/2020","1/22/2020","1/22/2020","1/22/2020",
                              "1/22/2020","1/22/2020","1/22/2020",
                              "1/22/2020","1/22/2020","1/22/2020","1/22/2020",
                              "1/22/2020","1/22/2020","1/22/2020","1/22/2020",
                              "1/22/2020","1/22/2020","1/22/2020","1/22/2020",
                              "1/22/2020","1/22/2020","1/22/2020","1/22/2020",
                              "1/22/2020","1/22/2020","1/22/2020","1/22/2020",
                              "1/22/2020","1/22/2020","1/23/2020",
                              "1/23/2020","1/23/2020","1/23/2020","1/23/2020",
                              "1/23/2020","1/23/2020","1/23/2020","1/23/2020",
                              "1/23/2020"),
         `Province/State` = c("Anhui",
                              "Beijing","Chongqing","Fujian","Gansu","Guangdong",
                              "Guangxi","Guizhou","Hainan","Hebei",
                              "Heilongjiang","Henan","Hong Kong","Hubei","Hunan",
                              "Inner Mongolia","Jiangsu","Jiangxi","Jilin",
                              "Liaoning","Macau","Ningxia","Qinghai","Shaanxi",
                              "Shandong","Shanghai","Shanxi","Sichuan",
                              "Taiwan","Tianjin","Tibet","Washington","Xinjiang",
                              "Yunnan","Zhejiang",NA,NA,NA,"Anhui",
                              "Beijing","Chongqing","Fujian","Gansu","Guangdong",
                              "Guangxi","Guizhou","Hainan","Hebei"),
         `Country/Region` = c("Mainland China",
                              "Mainland China","Mainland China",
                              "Mainland China","Mainland China","Mainland China",
                              "Mainland China","Mainland China","Mainland China",
                              "Mainland China","Mainland China","Mainland China",
                              "Hong Kong","Mainland China","Mainland China",
                              "Mainland China","Mainland China","Mainland China",
                              "Mainland China","Mainland China","Macau",
                              "Mainland China","Mainland China","Mainland China",
                              "Mainland China","Mainland China",
                              "Mainland China","Mainland China","Taiwan","Mainland China",
                              "Mainland China","US","Mainland China",
                              "Mainland China","Mainland China","Japan","Thailand",
                              "South Korea","Mainland China","Mainland China",
                              "Mainland China","Mainland China",
                              "Mainland China","Mainland China","Mainland China",
                              "Mainland China","Mainland China","Mainland China"),
              Last.Update = c("1/22/2020 17:00",
                              "1/22/2020 17:00","1/22/2020 17:00",
                              "1/22/2020 17:00","1/22/2020 17:00","1/22/2020 17:00",
                              "1/22/2020 17:00","1/22/2020 17:00",
                              "1/22/2020 17:00","1/22/2020 17:00","1/22/2020 17:00",
                              "1/22/2020 17:00","1/22/2020 17:00","1/22/2020 17:00",
                              "1/22/2020 17:00","1/22/2020 17:00",
                              "1/22/2020 17:00","1/22/2020 17:00","1/22/2020 17:00",
                              "1/22/2020 17:00","1/22/2020 17:00","1/22/2020 17:00",
                              "1/22/2020 17:00","1/22/2020 17:00",
                              "1/22/2020 17:00","1/22/2020 17:00","1/22/2020 17:00",
                              "1/22/2020 17:00","1/22/2020 17:00",
                              "1/22/2020 17:00","1/22/2020 17:00","1/22/2020 17:00",
                              "1/22/2020 17:00","1/22/2020 17:00","1/22/2020 17:00",
                              "1/22/2020 17:00","1/22/2020 17:00",
                              "1/22/2020 17:00","1/23/2020 17:00","1/23/2020 17:00",
                              "1/23/2020 17:00","1/23/2020 17:00","1/23/2020 17:00",
                              "1/23/2020 17:00","1/23/2020 17:00",
                              "1/23/2020 17:00","1/23/2020 17:00","1/23/2020 17:00"),
                Confirmed = c(1L,14L,6L,1L,
                              0L,26L,2L,1L,4L,1L,0L,5L,0L,444L,4L,0L,
                              1L,2L,0L,2L,1L,1L,0L,0L,2L,9L,1L,5L,
                              1L,4L,0L,1L,0L,1L,10L,2L,2L,1L,9L,22L,
                              9L,5L,2L,32L,5L,3L,5L,1L),
                   Deaths = c(0L,0L,0L,0L,
                              0L,0L,0L,0L,0L,0L,0L,0L,0L,17L,0L,0L,0L,
                              0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,
                              0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,
                              0L,0L,0L,0L,0L,1L),
                Recovered = c(0L,0L,0L,0L,
                              0L,0L,0L,0L,0L,0L,0L,0L,0L,28L,0L,0L,0L,
                              0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,
                              0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,
                              0L,2L,0L,0L,0L,0L)
       )
#--- 
ui<-fluidPage(
  fluidRow(plotlyOutput("plott1")),
  fluidRow(plotlyOutput("plott2")),
  fluidRow(plotlyOutput("plott3")),
  fluidRow(dataTableOutput("x2"))
)

server<-function(input,output,session){
  
output$plott1 <- renderPlotly({
   layout <- list(
      font = list(size = 12),
      title = "sonography for other-state",
      xaxis = list(title = "district(other-state)"),
      yaxis = list(title = "sonography records",automargin = TRUE)
    )

p <- plot_ly(total, x = total$Province.State, y = total$Confirmed, type = 'bar', name = 'rajasthan', source = 'link1',marker = list(color = COL)) %>%
      layout(yaxis = list(title = 'sonography block_level'),xaxis = list(title = 'Block name'),title='Sonograhy For Rajasthan')
  })

output$plott2 <- renderPlotly({
 d<-event_data('plotly_click',source = 'link1')
 if(is.null(d)==F){
    layout <- list(
      font = list(size = 12),
      title = "sonography for other-state",
      xaxis = list(title = "district(other-state)"),
      yaxis = list(title = "sonography records",automargin = TRUE)
    )

    p <- plot_ly(total, colors = COL, marker = list(color = COL),source = "link10") %>%
     add_trace(x = total$Country.Region, y = total$Confirmed,name = "",type = 'bar') %>%
     layout(font = layout$font, title = layout$title, xaxis = layout$xaxis, yaxis = layout$yaxis)
   
    }
 else
 {
   return(NULL)
 }
 })
output$plott3 <- renderPlotly({
    d<-event_data('plotly_click',source = 'link10')
    if(is.null(d)==F){
    p <- plot_ly(total, x = total$Country.Region, y =total$Deaths , type = 'bar', name = 'rajasthan',source = "hey",
                 marker = list(color = '#1a3e59',
                               line = list(color = 'rgba(246, 78, 139, 1.0)'))) %>%
      layout(barmode = 'stack',
             xaxis = list(title = "Centre Data"),
             yaxis = list(title ="Centre Name"))}
    else{return(NULL)}
  })
output$x2 <- DT::renderDataTable({
    d<-event_data('plotly_click',source='hey')
    if(is.null(d)==F){
    subdata <- select(total$ObservationDate,total$Province.State,total$Country.Region,total$Last.Update,total$Confirmed,total$Deaths,total$Recovered)
   
    DT::datatable(extensions = 'Buttons',subdata[,],options = list(dom = 'Blfrtip',
    buttons =
      list('copy', 'print', list(
        extend = 'collection',
        buttons = c('csv', 'excel', 'pdf'),
        text = 'Download'
      )),initComplete = JS(
    "function(settings, json) {",
    "$(this.api().table().header()).css({'background-color': '#090057', 'color': '#fff'});",
    "}"),lengthMenu = c(5,10,15,20), pageLength = 10,scrollX = TRUE),class = "nowrap cell-border hover stripe",editable = FALSE)
    }
    else{
      return(NULL)
    }
  })
}

shinyApp(ui,server)

Some evident issues:

  • province/state is a non-syntactic variable name so you have to reference it between backticks (e.g. `province/state`).
  • . ~ total$province/state this means that you want to aggregate all other variables by province/state but you have character variables also and you can't sum text. To solve this, select the numeric variables you want to sum.
  • And last but not least, please make reproducible examples that are specific to your problem, Making aggregations is not related to shiny apps or all the other code you have posted.
total<-data.frame(
    stringsAsFactors = FALSE,
    check.names = FALSE,
    SNo = c(1L,2L,3L,4L,
            5L,6L,7L,8L,9L,10L,11L,12L,13L,14L,15L,
            16L,17L,18L,19L,20L,21L,22L,23L,24L,25L,
            26L,27L,28L,29L,30L,31L,32L,33L,34L,35L,
            36L,37L,38L,39L,40L,41L,42L,43L,44L,45L,
            46L,47L,48L),
    ObservationDate = c("1/22/2020",
                        "1/22/2020","1/22/2020","1/22/2020","1/22/2020",
                        "1/22/2020","1/22/2020","1/22/2020","1/22/2020",
                        "1/22/2020","1/22/2020","1/22/2020","1/22/2020",
                        "1/22/2020","1/22/2020","1/22/2020",
                        "1/22/2020","1/22/2020","1/22/2020","1/22/2020",
                        "1/22/2020","1/22/2020","1/22/2020","1/22/2020",
                        "1/22/2020","1/22/2020","1/22/2020","1/22/2020",
                        "1/22/2020","1/22/2020","1/22/2020","1/22/2020",
                        "1/22/2020","1/22/2020","1/22/2020","1/22/2020",
                        "1/22/2020","1/22/2020","1/23/2020",
                        "1/23/2020","1/23/2020","1/23/2020","1/23/2020",
                        "1/23/2020","1/23/2020","1/23/2020","1/23/2020",
                        "1/23/2020"),
    `Province/State` = c("Anhui",
                         "Beijing","Chongqing","Fujian","Gansu","Guangdong",
                         "Guangxi","Guizhou","Hainan","Hebei",
                         "Heilongjiang","Henan","Hong Kong","Hubei","Hunan",
                         "Inner Mongolia","Jiangsu","Jiangxi","Jilin",
                         "Liaoning","Macau","Ningxia","Qinghai","Shaanxi",
                         "Shandong","Shanghai","Shanxi","Sichuan",
                         "Taiwan","Tianjin","Tibet","Washington","Xinjiang",
                         "Yunnan","Zhejiang",NA,NA,NA,"Anhui",
                         "Beijing","Chongqing","Fujian","Gansu","Guangdong",
                         "Guangxi","Guizhou","Hainan","Hebei"),
    `Country/Region` = c("Mainland China",
                         "Mainland China","Mainland China",
                         "Mainland China","Mainland China","Mainland China",
                         "Mainland China","Mainland China","Mainland China",
                         "Mainland China","Mainland China","Mainland China",
                         "Hong Kong","Mainland China","Mainland China",
                         "Mainland China","Mainland China","Mainland China",
                         "Mainland China","Mainland China","Macau",
                         "Mainland China","Mainland China","Mainland China",
                         "Mainland China","Mainland China",
                         "Mainland China","Mainland China","Taiwan","Mainland China",
                         "Mainland China","US","Mainland China",
                         "Mainland China","Mainland China","Japan","Thailand",
                         "South Korea","Mainland China","Mainland China",
                         "Mainland China","Mainland China",
                         "Mainland China","Mainland China","Mainland China",
                         "Mainland China","Mainland China","Mainland China"),
    Last.Update = c("1/22/2020 17:00",
                    "1/22/2020 17:00","1/22/2020 17:00",
                    "1/22/2020 17:00","1/22/2020 17:00","1/22/2020 17:00",
                    "1/22/2020 17:00","1/22/2020 17:00",
                    "1/22/2020 17:00","1/22/2020 17:00","1/22/2020 17:00",
                    "1/22/2020 17:00","1/22/2020 17:00","1/22/2020 17:00",
                    "1/22/2020 17:00","1/22/2020 17:00",
                    "1/22/2020 17:00","1/22/2020 17:00","1/22/2020 17:00",
                    "1/22/2020 17:00","1/22/2020 17:00","1/22/2020 17:00",
                    "1/22/2020 17:00","1/22/2020 17:00",
                    "1/22/2020 17:00","1/22/2020 17:00","1/22/2020 17:00",
                    "1/22/2020 17:00","1/22/2020 17:00",
                    "1/22/2020 17:00","1/22/2020 17:00","1/22/2020 17:00",
                    "1/22/2020 17:00","1/22/2020 17:00","1/22/2020 17:00",
                    "1/22/2020 17:00","1/22/2020 17:00",
                    "1/22/2020 17:00","1/23/2020 17:00","1/23/2020 17:00",
                    "1/23/2020 17:00","1/23/2020 17:00","1/23/2020 17:00",
                    "1/23/2020 17:00","1/23/2020 17:00",
                    "1/23/2020 17:00","1/23/2020 17:00","1/23/2020 17:00"),
    Confirmed = c(1L,14L,6L,1L,
                  0L,26L,2L,1L,4L,1L,0L,5L,0L,444L,4L,0L,
                  1L,2L,0L,2L,1L,1L,0L,0L,2L,9L,1L,5L,
                  1L,4L,0L,1L,0L,1L,10L,2L,2L,1L,9L,22L,
                  9L,5L,2L,32L,5L,3L,5L,1L),
    Deaths = c(0L,0L,0L,0L,
               0L,0L,0L,0L,0L,0L,0L,0L,0L,17L,0L,0L,0L,
               0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,
               0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,
               0L,0L,0L,0L,0L,1L),
    Recovered = c(0L,0L,0L,0L,
                  0L,0L,0L,0L,0L,0L,0L,0L,0L,28L,0L,0L,0L,
                  0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,
                  0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,
                  0L,2L,0L,0L,0L,0L)
)

aggregate(cbind(Confirmed, Deaths, Recovered) ~ `Province/State`, data = total, sum)
#>    Province/State Confirmed Deaths Recovered
#> 1           Anhui        10      0         0
#> 2         Beijing        36      0         0
#> 3       Chongqing        15      0         0
#> 4          Fujian         6      0         0
#> 5           Gansu         2      0         0
#> 6       Guangdong        58      0         2
#> 7         Guangxi         7      0         0
#> 8         Guizhou         4      0         0
#> 9          Hainan         9      0         0
#> 10          Hebei         2      1         0
#> 11   Heilongjiang         0      0         0
#> 12          Henan         5      0         0
#> 13      Hong Kong         0      0         0
#> 14          Hubei       444     17        28
#> 15          Hunan         4      0         0
#> 16 Inner Mongolia         0      0         0
#> 17        Jiangsu         1      0         0
#> 18        Jiangxi         2      0         0
#> 19          Jilin         0      0         0
#> 20       Liaoning         2      0         0
#> 21          Macau         1      0         0
#> 22        Ningxia         1      0         0
#> 23        Qinghai         0      0         0
#> 24        Shaanxi         0      0         0
#> 25       Shandong         2      0         0
#> 26       Shanghai         9      0         0
#> 27         Shanxi         1      0         0
#> 28        Sichuan         5      0         0
#> 29         Taiwan         1      0         0
#> 30        Tianjin         4      0         0
#> 31          Tibet         0      0         0
#> 32     Washington         1      0         0
#> 33       Xinjiang         0      0         0
#> 34         Yunnan         1      0         0
#> 35       Zhejiang        10      0         0

Created on 2020-10-19 by the reprex package (v0.3.0)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.