英文:
Sum of columns with similar column name
问题
我需要包含字符串“ABC DEF”的列的总和(我正在使用dplyr工作)。
df <- data.frame("aaa" = 2:5, "bbb" = 1:4, "ABC_DEF" = 1:4, "DEF" = 2:5, "ABC_DEF_GHI" = 3:6, "aaa_ABC_DEF" = 2:5)
df %>%
mutate(ABC_DEF = rowSums(select(., contains("ABC_DEF"))))
期望的输出:
aaa bbb ABC_DEF DEF ABC_DEF_GHI aaa_ABC_DEF ABC_DEF
1 2 1 1 2 3 2 6
2 3 2 2 3 4 3 9
3 4 3 3 4 5 4 12
4 5 4 4 5 6 5 15
英文:
I have multiple columns. Some of them contain a certain string, say "ABC DEF".
I need the sum of the columns containing this string (I'm working with dplyr).
df <- data.frame("aaa" = 2:5, "bbb" = 1:4, "ABC_DEF" = 1:4, "DEF" = 2:5, "ABC_DEF_GHI" = 3:6, "aaa_ABC_DEF" = 2:5)
aaa bbb ABC_DEF DEF ABC_DEF_GHI aaa_ABC_DEF
1 2 1 1 2 3 2
2 3 2 2 3 4 3
3 4 3 3 4 5 4
4 5 4 4 5 6 5
I tried something like this:
df %>%
mutate(ABC_DEF = sum(select(c(contains("ABC_DEF")))))
With this I get the error : ! contains()
must be used within a selecting function.
Desired output:
aaa bbb ABC_DEF_G DEF ABC_DEF_GHI aaa_ABC_DEF ABC_DEF
1 2 1 1 2 3 2 6
2 3 2 2 3 4 3 9
3 4 3 3 4 5 4 12
4 5 4 4 5 6 5 15
Can anyone help me how I could do it?
答案1
得分: 1
你可以使用rowwise
和c_across
的组合来完成这个任务。
library(dplyr)
df %>% rowwise() %>%
mutate(ABC.DEF.1 = sum(c_across(contains("ABC.DEF")))) %>%
ungroup()
# 一个数据框: 4 行 x 7 列
aaa bbb ABC.DEF DEF ABC.DEF.GHI aaa.ABC.DEF ABC.DEF.1
<int> <int> <int> <int> <int> <int> <int>
1 2 1 1 2 3 2 6
2 3 2 2 3 4 3 9
3 4 3 3 4 5 4 12
4 5 4 4 5 6 5 15
英文:
You can use a combination of rowwise
and c_across
to do the job.
library(dplyr)
df %>% rowwise() %>%
mutate(ABC.DEF.1 = sum(c_across(contains("ABC.DEF")))) %>%
ungroup()
# A tibble: 4 × 7
aaa bbb ABC.DEF DEF ABC.DEF.GHI aaa.ABC.DEF ABC.DEF.1
<int> <int> <int> <int> <int> <int> <int>
1 2 1 1 2 3 2 6
2 3 2 2 3 4 3 9
3 4 3 3 4 5 4 12
4 5 4 4 5 6 5 15
答案2
得分: 1
使用dplyr
和rowSums()
,你可以得到以下输出:
ABC.DEF ABC.DEF.GHI aaa.ABC.DEF ABC.DEF.SUM
1 1 3 2 6
2 2 4 3 9
3 3 5 4 12
4 4 6 5 15
请注意,这是代码的输出结果。
英文:
With dplyr
and rowSums()
,
require(dplyr)
df <- data.frame("aaa" = 2:5, "bbb" = 1:4, "ABC DEF" = 1:4, "DEF" = 2:5, "ABC DEF GHI" = 3:6, "aaa ABC DEF" = 2:5)
df %>%
select(contains('ABC.DEF')) %>%
mutate(ABC.DEF.SUM = rowSums(across(everything())))
Output
ABC.DEF ABC.DEF.GHI aaa.ABC.DEF ABC.DEF.SUM
1 1 3 2 6
2 2 4 3 9
3 3 5 4 12
4 4 6 5 15
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论