编辑:固定!
我试图从一个.xy文件(文本文件)中导入两列数据,如下所示:
(title "RMSE Velocity Magnitude")
(labels "Position" "RMSE Velocity Magnitude")
((xy/key/label "rake-9")
-12 3.52859
-11.985 3.53129
-11.97 3.534
-11.955 3.5367
-11.9399 3.5394
-11.9249 3.5421
-11.9099 3.5448
-11.8949 3.54751
-11.8799 3.55021
-11.8649 3.55291
-11.8499 3.55561
-11.8348 3.55831
)我想跳过前5行,导入数据,底部没有括号。后者使我头痛。
到目前为止,我有以下几点:
strs <- readLines("file.xy")
dat <- read.table(text=strs, # read from an R object rather than a file
skip=5, # skip the first line
nrows=length(strs) - 6 # skip the last line
)发布于 2016-04-20 18:33:31
好吧,你可以从字符串中去掉结尾的括号,然后读出来。
strs <-
'(title "RMSE Velocity Magnitude")
(labels "Position" "RMSE Velocity Magnitude")
((xy/key/label "rake-9")
-12 3.52859
-11.985 3.53129
-11.97 3.534
-11.955 3.5367
-11.9399 3.5394
-11.9249 3.5421
-11.9099 3.5448
-11.8949 3.54751
-11.8799 3.55021
-11.8649 3.55291
-11.8499 3.55561
-11.8348 3.55831
)'
strs <- gsub("\n)","",strs)
dat <- read.table(text=strs, skip=4)注意,有4,而不是5,标题行跳过,所以我使用了一个skip=4在这个例子。
https://stackoverflow.com/questions/36751633
复制相似问题