删除数据框每一列中的相同值,并指定值在给定列中的位置

Delete the same value in each column of the dataframe and specify the positions of the values in the given columns

首先: 我需要一些提示如何最快地完成它,因为我想将它多次应用于具有很多行的数据框。 我想删除数据框每一列中的相同值。 数据框的每一列都是给定因子的排列,无需替换。

例如,我从每列中删除值“1”:

column<-1:20
cbind(sample(column))
data <- matrix(column , length(column) , 5)
data<-apply(data,2, sample)
for (n in 1:length(data[1, ])) {
  data[, n]<-c(data[-which(data[,n]==1), n], 1)
}
data <- data[-length(data[,1]),]

第二个: 我想指定给定列中值相对于第一列的位置。

pos <- function(data){
  Position <- match(data[,1],data[,1])
  Position <- as.data.frame(Position)
  for (i in 2:length(data[1,])) {
    Position <- cbind(Position, match(data[,1],data[,i]))
  }
  return(Position)
}

如果您有任何更快的建议,请随时在下方提及。

第 1 部分

您可以按列进行 apply

apply(data, 2, function(x) x[-which(x == 1)])
#     [,1] [,2] [,3] [,4] [,5]
# [1,]   13   12    5    3   19
# [2,]    8   20    8   17   20
# [3,]   17    4   11   10    2
# [4,]   20    2   13   16    4
# [5,]    4   16   12    4   10
# [6,]   14    8   19   20    7
# [7,]    9    9    3   15    8
# [8,]    5   10    2   14   15
# [9,]    3   13   15    5   12
#[10,]   15    6   16    9   18
#[11,]   12   15   10    6   11
#[12,]   11    3    7   12   13
#[13,]    2    5   17   19   16
#[14,]    6    7    9   18    6
#[15,]   16   17    6   11   17
#[16,]   10   14   18    7   14
#[17,]   18   11   20    8    9
#[18,]   19   19    4    2    3
#[19,]    7   18   14   13    5

第 2 部分

cbind(1:nrow(data), apply(data[, -1], 2, function(x) match(data[, 1], x)))
#      [,1] [,2] [,3] [,4] [,5]
# [1,]    1   10    4   20   13
# [2,]    2    6    2   18    8
# [3,]    3   16   13    2   16
# [4,]    4    2   17    6    2
# [5,]    5    3   18    5    4
# [6,]    6   17   20    8   17
# [7,]    7    8   14   11   18
# [8,]    8   14    1    9   20
# [9,]    9    7   19   10    6
#[10,]   10   13    7    1   19
#[11,]   11   12    9    7    9
#[12,]   12    1    5   13   10
#[13,]   13   18    3   16   12
#[14,]   14    4    8   19    3
#[15,]   15   11   15   12   15
#[16,]   16    5   10    4   14
#[17,]   17    9   11    3    5
#[18,]   18   20   16   15   11
#[19,]   19   19    6   14    1
#[20,]   20   15   12   17    7

我们确认第 1 列中的第一个条目 (=13) 与第 2 列中的第 10 个条目匹配,与第 3 列中的第 4 个条目匹配,依此类推。


示例数据

set.seed(2017)
column<-1:20
cbind(sample(column))
data <- matrix(column , length(column) , 5)
data<-apply(data,2, sample)
data
#      [,1] [,2] [,3] [,4] [,5]
# [1,]   13   12    5    3   19
# [2,]    8   20    8   17   20
# [3,]   17    4   11   10    2
# [4,]   20    2   13   16    4
# [5,]    4   16   12    4   10
# [6,]   14    8   19   20    1
# [7,]    9    1    3   15    7
# [8,]    5    9    2   14    8
# [9,]    1   10   15    5   15
#[10,]    3   13   16    1   12
#[11,]   15    6   10    9   18
#[12,]   12   15    7    6   11
#[13,]   11    3   17   12   13
#[14,]    2    5    9   19   16
#[15,]    6    7    6   18    6
#[16,]   16   17   18   11   17
#[17,]   10   14   20    7   14
#[18,]   18   11    4    8    9
#[19,]   19   19    1    2    3
#[20,]    7   18   14   13    5

向量化函数:

 structure(data[data!=1],.Dim=dim(data)-c(1,0))

为了能够匹配我们可以使用:

 data1 = matrix(data[,1],nrow(data),ncol(data))

 array(pmatch(data1,data),dim(data))-(col(data)-1)*nrow(data)