从上一列中减去一列

Subtract one column from previous column

示例数据

dfData <- data.frame(ID = c(1, 2, 3, 4, 5), 
                  DistA = c(10, 8, 15, 22, 15), 
                 DistB = c(15, 35, 40, 33, 20),
                 DistC = c(20,40,50,45,30),
                 DistD = c(60,55,55,48,50))


   ID DistA DistB DistC DistD
 1  1    10    15    20    60
 2  2     8    35    40    55
 3  3    15    40    50    55
 4  4    22    33    45    48
 5  5    15    20    30    50

我有一些 ID,其中有四列测量累积距离。我想创建一个新列来给出每列的实际距离,即从上一列中减去下一列。 例如table 应该是这样的:

       ID DistA DistB DistC DistD
   1   1    10     5    5     40
   2   2     8    27    5     15
   3   3    15    25    10    5
   4   4    22    11    12    3 
   5   5    15    5     10    20

较长的方法是

dfData$disA <- dfData$DistA
dfData$disB <- dfData$DistB - dfData$DistA
dfData$disC <- dfData$DistC - dfData$DistB
dfData$disD <- dfData$DistD - dfData$DistC

是否有更短的方法来执行此操作,例如:

  apply(dfData,1,function(x) ???)

我们可以使用矢量化选项

dfData[3:5] <- dfData[-(1:2)] -  dfData[-c(1, 5)]
dfData
#  ID DistA DistB DistC DistD
#1  1    10     5     5    40
#2  2     8    27     5    15
#3  3    15    25    10     5
#4  4    22    11    12     3
#5  5    15     5    10    20

如果更新后的值用于顺序计算,那么for循环将很有用

nm1 <- names(dfData)[-1]
for(i in 1:3) dfData[[nm1[i+1]]] <- dfData[[nm1[i+1]]] - dfData[[nm1[i]]]

是的,你想要diff...

dfData[,-c(1:2)] <- t(apply(dfData[,-1], 1, diff))

dfData
  ID DistA DistB DistC DistD
1  1    10     5     5    40
2  2     8    27     5    15
3  3    15    25    10     5
4  4    22    11    12     3
5  5    15     5    10    20

一个简单的方法可以是:

dfData[3:5] = dfData[3:5] - dfData[(3:5) - 1]
#    ID DistA  DistB DistC DistD
# 1  1    10     5     5    40
# 2  2     8    27     5    15
# 3  3    15    25    10     5
# 4  4    22    11    12     3
# 5  5    15     5    10    20

与@akrun 提供的一个选项非常相似。