Reputation: 75
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
Upvotes: 0
Views: 1558
Reputation: 100
aggregate(total ~ Name + NumberB,data = df1, FUN = sum)
OR
aggregate(df1$total, by = list(df1$Name, df1$NumberB), FUN = sum)
Both are equivalent
Upvotes: 1
Reputation: 887068
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)
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))
Upvotes: 1
Reputation: 2849
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)
Upvotes: 2