[英]Extracting from text file in R
I need to compare two .txt files with the following formats , with R: 我需要将以下格式的两个.txt文件与R进行比较:
rows in file1: file1中的行:
1-11!AIVDM,1,1,,B,11b4N?@P?w<tSF0l4Q@>4?wp1`Oo,0*3D
1347204643
2-12$GPRMC,153102,A,6300.774,N,05238.627,W,12.9,186,090912,30,W*79
1347204664
( here for some reason the time (1347204643) is in the separate row) (由于某种原因,时间(1347204643)在单独的行中)
rows in file2: file2中的行:
#1:1347204643:11!AIVDM,1,1,,B,11b4N?@P?w<tSF0l4Q@>4?wp1`Oo,0*3D
#2:1347204664:12$GPRMC,153102,A,6300.774,N,05238.627,W,12.9,186,090912,30,W*79
I am interested only in verifying if the same ID, which is in the beginning of the row (eg 1 and 2 here), exists in both files ( if the ID that exists in file1 exists also in file2). 我只对验证两个文件中是否存在同一行开头(例如此处的1和2)相同的ID(如果file1中存在的ID也存在于file2中)感兴趣。
Can someone help me with this? 有人可以帮我弄这个吗? Thank you very much in advance! 提前非常感谢您!
You can do something like this : 您可以执行以下操作:
First you read 2 two files using readLines
首先,您使用readLines
读取2个两个文件
ll1 <- readLines(textConnection('#1:1347204643:11!AIVDM,1,1,,B,11b4N?@P?w<tSF0l4Q@>4?wp1`Oo,0*3D
#2:1347204664:12$GPRMC,153102,A,6300.774,N,05238.627,W,12.9,186,090912,30,W*79'))
ll2 <- readLines(textConnection('1-11!AIVDM,1,1,,B,11b4N?@P?w<tSF0l4Q@>4?wp1`Oo,0*3D
1347204643
2-12$GPRMC,153102,A,6300.774,N,05238.627,W,12.9,186,090912,30,W*79
1347204664'))
Do some treatments 做一些治疗
#Remove '#` fom the first files
ll1 <- gsub('#','',ll1)
#Take only the odd lines from the second file
ll2 <- ll2[c(TRUE,FALSE)]
Extract the index of each lines using substr
使用substr
提取每行的索引
ll1 <- substr(ll1,1,1)
ll2 <- substr(ll2,1,1)
Now you have this 2 lists : 现在您有以下两个列表:
ll1
[1] "1" "2"
> ll2
[1] "1" "2
To compare you can use match
要进行比较,您可以使用match
match(ll1,ll2)
[1] 1 2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.