我有一个文件(Map_1.hdr)从乐器生成,这里是文件:
ENVI
description = {ROI id #1}
samples = 16
lines = 4
bands = 1025
data type = 4
interleave = bip
wavelength =
pixel size = {9.38E-07, 7.5E-07}
x-start and y-start = {0.027363358, -0.007902135}
我需要从最后两行获取特定数据,这些数据:
pixel_size = c(9.38E-07,7.5E-07)
origin = (0.027363358, -0.007902135)
这是我的(不完整)尝试:
library(R.utils)
rem <- 2
nL <- countLines("Map_1.hdr")
df <- read.csv("Map_1.hdr", header=FALSE, sep=" ", skip=nL-rem, stringsAsFactors = FALSE)
有了这个,我得到了最后两排,但我仍然很远,以清理其余的。有没有其他方法可以得到我想要的东西?
答案 0 :(得分:1)
这就是我用过的东西:
txt <-" ENVI
description = {ROI id #1}
samples = 16
lines = 4
bands = 1025
data type = 4
interleave = bip
wavelength =
pixel size = {9.38E-07, 7.5E-07}
x-start and y-start = {0.027363358, -0.007902135}"
rem <- 2
nL <- length(readLines(textConnection(txt)))
df <- read.delim(text=gsub(patt = "^.+\\{|\\}",
# ^^^^^^ removes everything upto last '{'
# ^^^ as well as the trailing '}'
# ^ the `|` char is regex logical OR
repl = "", # by replacing with length zero character
readLines(textConnection(txt))), # input text or file
header=FALSE, sep=",", # left the comma in so it can be 'sep'
skip=nL-rem, stringsAsFactors = FALSE)
> df
V1 V2
1 0.000000938 0.000000750
2 0.027363358 -0.007902135
您可以使用文件名替换readLines(textConnection(txt))
的实例,并删除text =参数。 (它对构建可工作的,可测试的例子非常有用。)
答案 1 :(得分:1)
这可行吗?不确定我是否完全理解你想要的输出:
>attempt <- read.table("~/"Map_1.hdr"", sep= "=", stringsAsFactors = F)
> tail(attempt,2)$ENVI
[1] " {9.38E-07, 7.5E-07}" " {0.027363358, -0.007902135}"
> tail(attempt,2)$ENVI[1]
[1] " {9.38E-07, 7.5E-07}"
> tail(attempt,2)$ENVI[2]
[1] " {0.027363358, -0.007902135}"
然后,您可以使用strsplit
和gsub
从那里获得所需内容?
> strsplit(gsub('[\\{}]', "", tail(attempt,2)$ENVI[1]),",")[[1]][1]
[1] " 9.38E-07"
> strsplit(gsub('[\\{}]', "", tail(attempt,2)$ENVI[1]),",")[[1]][2]
[1] " 7.5E-07"