作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我希望将 .tps 文件读入 R。
现在可以在以下位置找到示例文件:
example file
我试图读入 R 的实际文件显然有更多的个人/ID(> 1000)
.tps 文件格式由 TPSSIG 生成。
http://life.bio.sunysb.edu/morph/
该文件是 ANSI 纯文本文件。
该文件包含 X 和 Y 坐标以及试样信息,如下所示。
主要的困难是样本的属性数量不同(例如,有些有 4 个,有些有 6 个 LM 地标,有些有 2 条曲线,其他没有,因此没有关联点)。
我曾尝试使用 for 循环和 read.table,但找不到一种方法来解释不同数量的属性。
文件开头示例
LM=3
1 1
2 2
3 3
CURVES=2
POINTS=2
1 1
2 2
POINTS=2
1 1
2 2
IMAGE=COMPLETE/FILE/PATH/IMAGE
ID=1
SCALE=1
LM=3
1 1
2 2
3 3
CURVES=2
...
i<-1
landmarks<-NULL
while(i < 4321){
print(i)
landmarks.temp<-read.table(file="filepath", sep=" ", header=F, skip=i, nrows=12, col.names=c("X", "Y"))
i<-i+13
landmarks.temp$ID<-read.table(file="filepath", sep=c(" "), header=F, skip=i, nrows=1, as.is=T)[1,1]
i<-i+1
landmarks.temp$scale<-read.table(file="filepath", sep=c(" "), header=F, skip=i, nrows=1, as.is=T)[1,1]
i<-i+2
landmarks<-rbind(landmarks, landmarks.temp)
print(unique(landmarks.temp$ID))
}
最佳答案
我不太清楚您在输出中寻找什么。我假设了一个标准数据框,其中 X、Y、ID 和 Scale 作为变量。
试试我拼凑的这个函数,看看它是否为您提供了您正在寻找的输出类型:
read.tps = function(data) {
a = readLines(data)
LM = grep("LM", a)
ID.ind = grep("ID", a)
images = basename(gsub("(IMAGE=)(.*)", "\\2", a[ID.ind - 1]))
skip = LM
nrows = as.numeric(gsub("(LM=)([0-9])", "\\2", grep("LM", a, value=T)))
l = length(LM)
landmarks = vector("list", l)
for (i in 1:l) {
landmarks[i] = list(data.frame(
read.table(file=data, header=F, skip=LM[i],
nrows=nrows[i], col.names=c("X", "Y")),
IMAGE = images[i],
ID = read.table(file=data, header=F, skip=ID.ind[i]-1,
nrows=1, sep="=", col.names="ID")[2,],
Scale = read.table(file=data, header=F, skip=ID.ind[i],
nrows=1, sep="=")[,2]))
}
do.call(rbind, landmarks)
}
read.tps("example.tps")
landmarks <- read.tps("example.tps")
关于将 .tps morphometrics 文件读入 R,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9729491/
我希望将 .tps 文件读入 R。 现在可以在以下位置找到示例文件: example file 我试图读入 R 的实际文件显然有更多的个人/ID(> 1000) .tps 文件格式由 TPSSIG 生
我是一名优秀的程序员,十分优秀!