0

I want to be able to summarise the following, but I'm not sure how to So the table is like this

tibble::tribble(
  ~Name, ~`Number B`, ~total,
    "e",       1,         3233,
    "e",       2,         3222,
    "f",       1,         377,
    "f",       2,         456,
    "t",       1,         3266,
    "e",       1,         44,
    "t",       1,         3266,
    "f",       2,         909
  )

So I want to be able to get the totals of number ones per name. There are 2 e's number 1's etc.The columns are Name, Number b and total

Similar table of numbers in the pic. The table didn't come out too well above here is a table

Eric
  • 2,699
  • 5
  • 17
thewal
  • 75
  • 6
  • Also, please take some time to review how to provide a properly formatted [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – Eric Mar 20 '21 at 21:21

3 Answers3

2

Here is a data.table approach:

Note: Thank you @Akrun for the data.

df1 <- structure(list(Name = c("e", "e", "f", "f", "t", "e", "t", "f"
), `Number B` = c(1L, 2L, 1L, 2L, 1L, 1L, 1L, 2L), total = c(3233L, 
3222L, 377L, 456L, 3266L, 44L, 3266L, 909L)), class = "data.frame", 
row.names = c(NA, 
-8L))


library(data.table)

setDT(df1)[, .(total_one = sum(total[`Number B` == 1])),
           by = Name][]

#>    Name total_one
#> 1:    e      3277
#> 2:    f       377
#> 3:    t      6532

Created on 2021-03-20 by the reprex package (v1.0.0)

Eric
  • 2,699
  • 5
  • 17
  • Same idea but having the filter out of the summation: `df1[,.(total = sum(total)),.(Name, `Number B`)][`Number B`==1,]` – Chriss Paul Mar 21 '21 at 03:32
1

We can group by 'Name' and get the sum of subset of 'total' based on the logical expression from 'Number B'

library(dplyr)
df1 %>% 
   group_by(Name) %>%
   summarise(NumberOne = sum(total[`Number B` == 1]), .groups = 'drop')

-output

# A tibble: 3 x 2
#   Name  NumberOne
#* <chr>     <int>
#1 e          3277
#2 f           377
#3 t          6532

Or with collapse

library(collapse)
collap(sbt(df1, `Number B` == 1, -`Number B`), ~Name, fsum)
#  Name total
#1    e  3277
#2    f   377
#3    t  6532

Or using base R

aggregate(total ~ Name, subset(df1, `Number B` == 1), sum)

data

df1 <- structure(list(Name = c("e", "e", "f", "f", "t", "e", "t", "f"
), `Number B` = c(1L, 2L, 1L, 2L, 1L, 1L, 1L, 2L), total = c(3233L, 
3222L, 377L, 456L, 3266L, 44L, 3266L, 909L)), class = "data.frame", 
row.names = c(NA, 
-8L))
akrun
  • 874,273
  • 37
  • 540
  • 662
  • Number B has a space in between r and B – thewal Mar 20 '21 at 20:54
  • 1
    @thewal - If akrun's solution addresses your question, please consider accepting it by clicking the checkmark. This will help future users with the same / similar problem you have. – Eric Mar 20 '21 at 21:19
  • 1
    Perfect, this works for me, thanks. Last night I was using 'Number B' and it didn't work. This morning, fresh eyes I used `Number B` and it worked – thewal Mar 21 '21 at 10:04
1
aggregate(total ~ Name + NumberB,data = df1, FUN = sum)

OR

aggregate(df1$total, by = list(df1$Name, df1$NumberB), FUN = sum)

Both are equivalent

mrbonewithgale
  • 100
  • 1
  • 7