-2

how to perform a sum of values for multiple rows with the same name in r and render a chart in plotly.i have tried a couple of methods like aggregate and tapply, none of them seems to be working for me, could anyone tell me where I am going wrong.

library(dplyr)
    #> 
    #> Attaching package: 'dplyr'
    #> The following objects are masked from 'package:stats':
    #> 
    #>     filter, lag
    #> The following objects are masked from 'package:base':
    #> 
    #>     intersect, setdiff, setequal, union
    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
    cdata1<-data.frame(stringsAsFactors=FALSE,
                     names = c("a", "a", "a", "a", "a", "a", "a", "a", "b", "b", "b", "b",
                               "b", "c", "c", "c", "d", "d", "d"),
                     values = c(12, 32, 43, 45, 21, 21, 21, 32, 43, 54, 65, 76, 87, 80, 78,
                                68, 68, 67, 57)
    )

    ui<-fluidPage(fluidRow(plotlyOutput("id1")),
                  fluidRow(plotlyOutput("id2"))
    )



    server<-function(input,output){

      output$id1<-renderPlotly({

        # a<-aggregate(cdata1$X2014,by=list(cdata1$States.UTs),FUN=sum)
        # plot_ly(cdata1,x=cdata1$States.UTs,y=cdata1$X2014)
        cdata1 %>%
          group_by(grp = rleid(cdata1$names)) %>% 
          summarise(names = first(cdata1$names), 
                    values = sum(cdata1$values)) %>%
          ungroup %>%
          select(-grp)
        plot_ly(cdata1,x=cdata1$names,y=cdata1$values)
      })

    }
    shinyApp(ui,server)

3 Answers3

2

Does this do the trick?

> aggregate(values ~ names, data = cdata1, FUN = sum)
  names values
1     a    227
2     b    325
3     c    226
4     d    192
Roman Luštrik
  • 69,533
  • 24
  • 154
  • 197
0

I am not sure if the code below is something you are looking for. Otherwise, the solution of aggreate() by @Roman Luštrik might be the one for your question.

data <- within(data, s <- ave(values, names, FUN = sum))

such that

> data
   names values   s
1      a     12 227
2      a     32 227
3      a     43 227
4      a     45 227
5      a     21 227
6      a     21 227
7      a     21 227
8      a     32 227
9      b     43 325
10     b     54 325
11     b     65 325
12     b     76 325
13     b     87 325
14     c     80 226
15     c     78 226
16     c     68 226
17     d     68 192
18     d     67 192
19     d     57 192

DATA

data<-data.frame(stringsAsFactors=FALSE,
                 names = c("a", "a", "a", "a", "a", "a", "a", "a", "b", "b", "b", "b",
                           "b", "c", "c", "c", "d", "d", "d"),
                 values = c(12, 32, 43, 45, 21, 21, 21, 32, 43, 54, 65, 76, 87, 80, 78,
                            68, 68, 67, 57)
)
ThomasIsCoding
  • 96,636
  • 9
  • 24
  • 81
  • thanks for your help, I really appreciate your help and this is very close to what I was looking for , I don't wanna create a new column for its total , all I wanna do to print the distinct names(single time)instead of multiple times and sum for each distinct value to create a bar-chart(and it is serving my purpose) but why is it printing white horizontal lines in barchart ,i want just a simple barchart instead – siddharth sharma Dec 17 '19 at 11:24
  • @siddharthsharma then `aggreate()` solution should work for your purpose – ThomasIsCoding Dec 17 '19 at 11:50
0
library(dplyr)
cdata1 %>% 
  group_by(names) %>% 
  summarise(values = sum(values))

# A tibble: 4 x 2
  names values
  <chr>  <dbl>
1 a        227
2 b        325
3 c        226
4 d        192
Adamm
  • 2,150
  • 22
  • 30