我有两个大文件,我试图将来自file_1第一列的信息与file_2的头文件匹配。有一点小细节,file_2的头部在开头有一些信息,每列都有所不同,但最后它有模式匹配。基本上,我必须找到file_1的'pattern'出现在文件2的列名的末尾,并输出data.frame和这些信息。
请参考下面的文件什么样子:在另一个文件(R,Unix)中将'pattern'从一个文件匹配到头文件的名称
**file_1** dim (757*3) the first column of the file_1 contains patterns
10001-101A3 a t
10008-101B6 b g
10235-104A1 c h
- - -
- - -
etc...
**file_2** dim (4120*1079)
blabla.10001.101A3 blbl.2348.101B6 trsdr.1111.111D2 gfder.10008.101B6 ....
12 1223 544 - -
132 23 3564 - -
14 223 33 - -
162 13 344 - -
**Desired output file-3:** I assume that the output size will be 4120*757
blabla.10001.101A3 gfder.10008.101B6 ....
12 - -
132 - -
14 - -
162 - -
我试图让与R(下面是我的脚本)的输出,但我也想了解我怎么能在Unix的做到这一点(我猜-awk和-grep可以帮助解决这个问题)。
这里是我的[R脚本:
table1=read.table("file2.tsv.gz", quote=NULL, sep='\t', header=T, fill=T)
table2=read.table("file1.txt", quote=NULL, sep='\t', header=T, fill=T)
# dim(table1 4120 * 1079) -> need to reduce amount of columns to 757
# dim(table2 757 * 3)
###### the header in table1 has following view 10001.101A3, thus we need to substitute '-' to '.' in pattern
### What to do:
### 1) Use gsub() function to substitute '-' by '.'
### 2) Use gsub() function to remove space in the end of string ' ' by ''
### 3) Find modified pattern in the end of column's name
### 4) Apply to the entire table
pattern=table2[,1] # '10001-101A3 ' '10008-101B6 '
for (x in pattern) {
ptn=gsub('-','.',x)
ptn1=gsub(' ','',ptn) # pattern to be matched'
# '10001.101A3' '10008.101B6'
find_match=table1[,(grepl('^.+ptn1$', header))]
final_tb=table1[,find_match]
}
我认为这个问题是在grepl()PTN1的数据表示功能,因为当我插入10001.101A3代替PTN1我得到答案的一个运行,但显然我需要循环它。
我也尝试过(ptn1),但仍然无法使它工作。我希望你的意见,以及任何想法如何可以不在Unix(我是非常基本的Unix用户,因此目前无法实现此任务)。小数据
df=data.frame(aa24.12a,dda43.23s,fds24.12a,sdf24.112f)
Z = C('24 -12a”,'43
########################线索 - 23S“)#模式aa24.12a fds24.12a aa24.12a.1 fds24.12a.1
1 2 34 2 34
2 3 2 3 2
3 4 1 4 1
4 56 3 56 3
5 3 5 3 5
header=colnames(df)
for (x in z){
ptn=gsub('-','.',x)
ptn1=gsub(' ','',ptn)# correct pattern
find_match=grep('^.+24.12a$', header)# find match of pattern in header
tbl=df[,find_match]
}
> tbl
aa24.12a fds24.12a
1 2 34
2 3 2
3 4 1
4 56 3
5 3 5
谢谢