0

Hi I have 3 data set with contains the items and counts. I need to add the all data sets and combine the count based on the item names. He is my input.

Df1 <- data.frame(items =c("Cookies", "Candys","Toys","Games"), Counts = c( 10,20,30,5))
Df2 <- data.frame(items =c( "Candys","Cookies","Toys"), Counts = c( 5,21,20))
Df3 <- data.frame(items =c( "Playdows","Gummies","Candys"), Counts = c(10,15,20))
Df_all <- rbind(Df1,Df2,Df3)

Df_all

      items Counts
1   Cookies     10
2    Candys     20
3      Toys     30
4     Games      5
5    Candys      5
6   Cookies     21
7      Toys     20
8  Playdows     10
9   Gummies     15
10   Candys     20

I need to combine the columns based on the item values. Delete the Row after adding the values. My output should be

    items   Counts
1   Cookies     31
2    Candys     45
3      Toys     50
4     Games      5
5  Playdows     10
6   Gummies     15

Could you help in getting this output in r.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291

2 Answers2

0

use dplyr:

    library(dplyr)
    result<-Df_all%>%group_by(items)%>%summarize(sum(Counts))
    > result
# A tibble: 6 x 2
  items    `sum(Counts)`
  <fct>            <dbl>
1 Candys           45.0 
2 Cookies          31.0 
3 Games             5.00
4 Toys             50.0 
5 Gummies          15.0 
6 Playdows         10.0   
Antonios
  • 1,919
  • 1
  • 11
  • 18
0

You can use tapply

tapply(Df_all$Counts, Df_all$items, FUN=sum)

what returns

  Candys  Cookies    Games     Toys  Gummies Playdows 
      45       31        5       50       15       10 
Adilson V Casula
  • 164
  • 1
  • 16