Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 用数字序列替换文件之间的匹配项_R_Bash_Loops - Fatal编程技术网

R 用数字序列替换文件之间的匹配项

R 用数字序列替换文件之间的匹配项,r,bash,loops,R,Bash,Loops,我有两个文本文件: FileA有三列: Col1 Col2 Col3 111111 111111 0 222222 222222 0 333333 333333 0 444444 444444 0 666666 666666 0 FileB有一列(无标题): 如果FileA的第1列和第2列中的内容与FileB中的内容匹配,我想替换它们。我希望替换为一系列负数,从-4开始 期望输出: Col1 Col2 Col3 111111 111111 0 -4 -4 0 3

我有两个文本文件:

FileA有三列:

Col1 Col2 Col3  
111111 111111 0  
222222 222222 0  
333333 333333 0  
444444 444444 0  
666666 666666 0
FileB有一列(无标题):

如果FileA的第1列和第2列中的内容与FileB中的内容匹配,我想替换它们。我希望替换为一系列负数,从-4开始

期望输出:

Col1 Col2 Col3  
111111 111111 0  
-4 -4 0  
333333 333333 0  
-5 -5 0  
-6 -6 0
文件A的实际长度约为500k,文件B为80


最好使用R或bash解决方案

使用base R,您可以这样做

FileA[] <- lapply(FileA, function(x){
  i <- match(FileB$Col1, x)
  if(all(!is.na(i))) x[i] <- -seq_along(i) - 3
  x
})

FileA
#    Col1   Col2 Col3
#1 111111 111111    0
#2     -4     -4    0
#3 333333 333333    0
#4     -5     -5    0
#5     -6     -6    0
#6     -7     -7    0

FileA[]这可以通过嵌套循环实现:

equalities <- apply(filea, 2, function(x) x %in% fileb)
result <- filea
replacement <- c(-4:-99)

for( i in 1:ncol(result)) {
  result[,i] <- ifelse(equalities[,i], "toreplace", result[,i])
  nbmatches <- 1
  for( j in 1:nrow(result)) {
    if("toreplace"==result[j,i]) nbmatches <- nbmatches + 1
    result[j,i] <- ifelse("toreplace"==result[j,i], replacement[nbmatches],result[j,i])
  }

  }
result
    Col1   Col2 Col3
1 111111 111111    0
2     -5     -5    0
3 333333 333333    0
4     -6     -6    0
5     -7     -7    0

equalities这假设两列具有相同的值

$ awk -v c=-4 'NR==FNR {a[$1]; next} 
               $1 in a {$1=$2=c--}1' fileB fileA 

Col1 Col2 Col3  
111111 111111 0  
-4 -4 0
333333 333333 0  
-5 -5 0
-6 -6 0
解释
将第一个文件值保存在数组
a
中。如果文件B的第一个字段在数组
a
中,则用计数器
c
替换第一个和第二个字段,并减小计数器。打印所有行(更新或未更新)。

谢谢。这在玩具示例中有效,但在我的实际文件中无效。也许这是因为我的实际文件没有排序?@oneill如果数据帧
FileA
没有排序,请替换
谢谢。由于某种原因,这花费了很长时间,我不得不终止这项工作——可能是因为我的文件中有太多行(>500k)
equalities <- apply(filea, 2, function(x) x %in% fileb)
result <- filea
replacement <- c(-4:-99)

for( i in 1:ncol(result)) {
  result[,i] <- ifelse(equalities[,i], "toreplace", result[,i])
  nbmatches <- 1
  for( j in 1:nrow(result)) {
    if("toreplace"==result[j,i]) nbmatches <- nbmatches + 1
    result[j,i] <- ifelse("toreplace"==result[j,i], replacement[nbmatches],result[j,i])
  }

  }
result
    Col1   Col2 Col3
1 111111 111111    0
2     -5     -5    0
3 333333 333333    0
4     -6     -6    0
5     -7     -7    0
$ awk -v c=-4 'NR==FNR {a[$1]; next} 
               $1 in a {$1=$2=c--}1' fileB fileA 

Col1 Col2 Col3  
111111 111111 0  
-4 -4 0
333333 333333 0  
-5 -5 0
-6 -6 0