Another dplyr solution. We can calculate the sum of each patent
to each column first using summarise_all(funs(sum(.)))
, and then we joined the result to the original data frame using left_join
. No need to specify individual column names.
library(dplyr)
df2 <- df %>%
group_by(patent) %>%
summarise_all(funs(sum(.))) %>%
setNames(sub("temp", "new", names(.))) %>%
left_join(df, ., by = "patent")
df2
# patent temp1 temp2 new1 new2
# 1 1 TRUE FALSE 1 0
# 2 2 FALSE TRUE 0 2
# 3 2 FALSE TRUE 0 2
Or we can use mutate_all
to calculate the sum and store the result to new columns.
df2 <- df %>%
group_by(patent) %>%
mutate_all(funs("sum" = sum(.))) %>%
ungroup()
df2
# # A tibble: 3 x 5
# patent temp1 temp2 temp1_sum temp2_sum
# <dbl> <lgl> <lgl> <int> <int>
# 1 1.00 T F 1 0
# 2 2.00 F T 0 2
# 3 2.00 F T 0 2
If the column names need to be exactly the same as the example, we can do the following.
df2 <- df %>%
group_by(patent) %>%
mutate_all(funs("sum" = sum(.))) %>%
ungroup() %>%
setNames(ifelse(grepl("_sum$", names(.)), sub("temp", "new", names(.)), names(.))) %>%
setNames(sub("_sum$", "", names(.)))
df2
# # A tibble: 3 x 5
# patent temp1 temp2 new1 new2
# <dbl> <lgl> <lgl> <int> <int>
# 1 1.00 T F 1 0
# 2 2.00 F T 0 2
# 3 2.00 F T 0 2