gpt4 book ai didi

将 .tps morphometrics 文件读入 R

转载 作者:行者123 更新时间:2023-12-04 05:54:08 25 4
gpt4 key购买 nike

我希望将 .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")

其中“example.tps”是工作目录中 .tps 文件的名称。

如果要将输出分配给新对象,可以使用标准:
landmarks <- read.tps("example.tps")

关于将 .tps morphometrics 文件读入 R,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9729491/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com