我们可以使用pmin
并pmax
执行该任务.它们将返回行数长度的向量,一个具有第二列和第三列之间的最小值,一个具有最大值.然后我们用新值替换原来的2列:
df[,2:3]<-cbind(pmin(df[,2],df[,3]),pmax(df[,2],df[,3])) df # V1 V2 V3 # 1 3 112634042 113634042 # 2 15 42130650 43130646 # 3 5 59947488 60947483 # 4 47 65968160 66968149 # 5 16 29674823 30674811 # 6 5 9441835 10441822
数据:
df <- structure(list(V1 = c(3L, 15L, 5L, 47L, 16L, 5L), V2 = c(113634042L, 42130650L, 59947488L, 65968160L, 30674811L, 10441822L), V3 = c(112634042L, 43130646L, 60947483L, 66968149L, 29674823L, 9441835L)), .Names = c("V1", "V2", "V3"), class = "data.frame", row.names = c(NA, -6L))
另一种选择是使用apply
和sort
:
t(apply(df[,2:3],1,sort)) # [,1] [,2] # [1,] 112634042 113634042 # [2,] 42130650 43130646 # [3,] 59947488 60947483 # [4,] 65968160 66968149 # [5,] 29674823 30674811 # [6,] 9441835 10441822
该apply
版本似乎是慢pmin
和pmax
:
Unit: microseconds expr min lq mean median uq max neval cld pmax 54.202 57.089 68.17331 68.4750 72.0025 167.738 100 a apply 561.263 581.950 624.66715 598.4675 630.5395 1179.936 100 b