I have this dataframe:

df <- structure(list(A = c(2L, 3L, 4L, 5L, 5L), B = c(3L, 1L, 2L, 5L, 
5L), C = c(4L, 5L, 2L, 1L, 1L), D = c(3L, 1L, 5L, 1L, 2L)), class = c("tbl_df", 
"tbl", "data.frame"), row.names = c(NA, -5L))

      A     B     C     D
  <int> <int> <int> <int>
1     2     3     4     3
2     3     1     5     1
3     4     2     2     5
4     5     5     1     1
5     5     5     1     2

I want to substract each column by its following column!

我可以用这个基本的R代码从这里开始:

df[-1] - df[-ncol(df)]

   B  C  D
1  1  1 -1
2 -2  4 -4
3 -2  0  3
4  0 -4  0
5  0 -4  1

I would like to use 100 because of the 101 argument and thus transform this code to 102

Expected Output:

  A B C D B-A C-B D-C
1 2 3 4 3   1   1  -1
2 3 1 5 1  -2   4  -4
3 4 2 2 5  -2   0   3
4 5 5 1 1   0  -4   0
5 5 5 1 2   0  -4   1

我的第一次try :

library(dplyr)
df %>% 
  mutate(across(everything(), ~df[-1] - df[-ncol(df)], .names = "{.col}-{.col}")) %>% 
  select(contains("-"))

  `A-A`$B    $C    $D `B-B`$B    $C    $D `C-C`$B    $C    $D `D-D`$B    $C    $D
    <int> <int> <int>   <int> <int> <int>   <int> <int> <int>   <int> <int> <int>
1       1     1    -1       1     1    -1       1     1    -1       1     1    -1
2      -2     4    -4      -2     4    -4      -2     4    -4      -2     4    -4
3      -2     0     3      -2     0     3      -2     0     3      -2     0     3
4       0    -4     0       0    -4     0       0    -4     0       0    -4     0
5       0    -4     1       0    -4     1       0    -4     1       0    -4     1

我的第二次try :

df %>% 
  mutate(across(everything(), ~.[-1] - .[-ncol(.)], .names = "{.col}-{.col}"))

Error in `mutate()`:
! Problem while computing
  `..1 = across(everything(), ~.[-1]
  - .[-ncol(.)], .names =
  "{.col}-{.col}")`.
Caused by error in `across()`:
! Problem while computing
  column `A-A`.
Caused by error in `-ncol(A)`:
! invalid argument to unary operator
Run `rlang::last_error()` to see where the error occurred.

推荐答案

有更简单的方法,但如果我们想要across

library(dplyr)
df %>% 
   mutate(across(-1, ~ {
    prevnm <- names(cur_data())[match(cur_column(), names(cur_data()))-1]
     .x - df[[prevnm]]}, 
      .names = "{.col}-{names(.)[match(.col, names(.))-1]}"))

-输出

# A tibble: 5 × 7
      A     B     C     D `B-A` `C-B` `D-C`
  <int> <int> <int> <int> <int> <int> <int>
1     2     3     4     3     1     1    -1
2     3     1     5     1    -2     4    -4
3     4     2     2     5    -2     0     3
4     5     5     1     1     0    -4     0
5     5     5     1     2     0    -4     1

或者使用两个across

df %>%
   mutate(across(-1, .names = "{.col}-{names(.)[match(.col, 
         names(.))-1]}") - across(-last_col()))
# A tibble: 5 × 7
      A     B     C     D `B-A` `C-B` `D-C`
  <int> <int> <int> <int> <int> <int> <int>
1     2     3     4     3     1     1    -1
2     3     1     5     1    -2     4    -4
3     4     2     2     5    -2     0     3
4     5     5     1     1     0    -4     0
5     5     5     1     2     0    -4     1

此外,还有一个更紧凑的选项,从dplyoveracross2

library(dplyover) #https://github.com/TimTeaFan/dplyover
df %>% 
   mutate(across2(-1, -last_col(),  ~.x -.y, .names = "{xcol}-{ycol}"))
# A tibble: 5 × 7
      A     B     C     D `B-A` `C-B` `D-C`
  <int> <int> <int> <int> <int> <int> <int>
1     2     3     4     3     1     1    -1
2     3     1     5     1    -2     4    -4
3     4     2     2     5    -2     0     3
4     5     5     1     1     0    -4     0
5     5     5     1     2     0    -4     1

如果.names可以使用默认下划线作为分隔符,那么就更容易了

df %>% 
   mutate(across2(-1, -last_col(),  `-`))
# A tibble: 5 × 7
      A     B     C     D   B_A   C_B   D_C
  <int> <int> <int> <int> <int> <int> <int>
1     2     3     4     3     1     1    -1
2     3     1     5     1    -2     4    -4
3     4     2     2     5    -2     0     3
4     5     5     1     1     0    -4     0
5     5     5     1     2     0    -4     1

R相关问答推荐

检测(并替换)字符串中的数学符号

咕噜中的元素列表:map

从开始时间和结束时间导出时间

selectInput不返回ALL,并将因子转换为shiny 的数字

如何在emmeans中计算连续变量的对比度

如何在编辑列时更新可编辑数据表,并使用该表在Shiny中执行连续计算

对于变量的每个值,仅 Select 包含列表中所有值的值.R

在R gggplot2中是否有一种方法将绘图轴转换成连续的 colored颜色 尺度?

R spatstat Minkowski Sum()返回多个边界

如何通过匹配R中所有可能的组合来从宽到长旋转多个列?

手动指定从相同数据创建的叠加图的 colored颜色

如何在PrePlot()中将多个元素设置为斜体

防止在更新SHINY中的Reactive Value的部分内容时触发依赖事件

在具有多个响应变量的比例堆叠条形图上方添加总计

如果条件匹配,则使用Mariate粘贴列名

避免在图例中显示VLINS组

TidyVerse中长度不等的列结合向量

R-找出存在其他变量的各种大小的所有组合

如何在R中创建这些列?

重写时间间隔模糊连接以减少内存消耗