如何在R中基于条件对多列进行减法

如何在R中基于条件对多列进行减法,r,R,在我的新数据集中,我想用InterestPad列和interestpaid列减去利息列。例如,如果有3个利息列、2个利息ID列和2个利息支付列,则必须从第一个利息ID列和利息支付列中减去第一个利息列,第二个为第二个,第三个为第三个。最后剩余的1个兴趣列必须保持原样 interest1 interest2 interest3 interestpaid1 interestpaid2 interestunpaid1 interestunpaid2 100 100

在我的新数据集中,我想用InterestPad列和interestpaid列减去利息列。例如,如果有3个利息列、2个利息ID列和2个利息支付列,则必须从第一个利息ID列和利息支付列中减去第一个利息列,第二个为第二个,第三个为第三个。最后剩余的1个兴趣列必须保持原样

interest1  interest2  interest3  interestpaid1  interestpaid2  interestunpaid1 interestunpaid2
100         100        150         50             50              10              20
200         150        100        100            100              20              30
300         250        10         100            150              30              40
400         100        200        100            150              40              50
我的输出应按以下所述进行计算。下面,I=利息,IP=利息ID,IUP=利息支付

output1               output2                  output3
40(I1-IP1-IUP1)     30(I2-IP2-IUP2)            150 (I3)
80                  20                         100
170                 60                         10
260                 -100                       200

一个
dplyr
purrr
选项可以是:

map_dfc(.x = unique(sub("\\D+", "", names(df))),
        ~ df %>%
         transmute(!!paste0("output", .x) := reduce(across(ends_with(.x)), `-`)))

  output1 output2 output3
1      40      30     150
2      80      20     100
3     170      60      10
4     260    -100     200
结果呢

  interest1 interest2 interest3
1        40        30       150
2        80        20       100
3       170        60        10
4       260      -100       200

这符合您的要求,但我会考虑让数据变长,这样您就不必指定每个输出,而只需使用group_by

library(tidyr)
library(dplyr)

Lines <- "interest1  interest2  interest3  interestpaid1  interestpaid2  interestunpaid1 interestunpaid2
100         100        150         50             50              10              20
200         150        100        100            100              20              30
300         250        10         100            150              30              40
400         100        200        100            150              40              50
"
data <-read.table(text = Lines, header = TRUE)

output <- mutate(data, output1 = interest1 - interestpaid1 - interestunpaid1,
                       output2 = interest2 - interestpaid2 - interestunpaid2,
                       output3 = interest3) %>%
  select(starts_with("output"))
library(tidyr)
图书馆(dplyr)
行%
突变(跨越(以“兴趣”开始,~replace_na(.x,0))%>%
突变(输出=利息-利息支付-利息支付)%>%
mutate(num=粘贴(“输出”,num))%>%
选择(obs、num、output)%>%
pivot\u更宽(id\u cols=obs,names\u from=num,values\u from=output)

如果我有近100列或更多列,该怎么办?所以我不能用这种方式陈述输出和减去100列。我已经更新了答案,使其对更多感兴趣的列具有鲁棒性,但是@tmfmnk的答案更好
library(tidyr)
library(dplyr)

Lines <- "interest1  interest2  interest3  interestpaid1  interestpaid2  interestunpaid1 interestunpaid2
100         100        150         50             50              10              20
200         150        100        100            100              20              30
300         250        10         100            150              30              40
400         100        200        100            150              40              50
"
data <-read.table(text = Lines, header = TRUE)

output <- mutate(data, output1 = interest1 - interestpaid1 - interestunpaid1,
                       output2 = interest2 - interestpaid2 - interestunpaid2,
                       output3 = interest3) %>%
  select(starts_with("output"))
output <- mutate(data, obs = row_number()) %>%
  pivot_longer(cols = 1:(ncol(data)), names_to = c("variable", "num"), names_sep = -1) %>%
  pivot_wider(names_from = variable, values_from = value) %>%
  mutate(across(starts_with("interest"), ~replace_na(.x, 0))) %>%
  mutate(output = interest - interestpaid - interestunpaid) %>%
  mutate(num = paste("output",num)) %>%
  select(obs, num, output) %>%
  pivot_wider(id_cols = obs, names_from = num, values_from = output)