我有一个包含24个TraMineR
序列对象的列表。现在我想计算每个序列对象的最佳匹配距离(仅在每个对象内),并将其存储在一个新的列表中,现在由24个OM距离对象(距离矩阵)组成。
可以找到数据集here。
library(TraMineR)
sequences <- read.csv(file = "event-stream-20-l-m.csv", header = TRUE, nrows=10)
repo_names = colnames(sequences)
# 1. Loop across and define the 24 sequence objects & store them in sequence_objects
colpicks <- seq(10,240,by=10)
sequence_objects <- mapply(function(start,stop) seqdef(sequences[,start:stop]), colpicks- 9, colpicks)
# 2. Calculate the costs for OM distances within each object
costs <- mapply(seqsubm(sequence_objects, method="TRATE"))
# 3. Calculate the OM distance objects for each sequence object
sequences.om <- seqdist(sequence_objects, method="OM", indel=1, sm=costs, with.missing=FALSE, norm="maxdist")
步骤(1)工作正常,但当我进入步骤(2)时,它告诉我:
Error in seqsubm(sequence_objects, method = "TRATE") :
[!] data is NOT a sequence object, see seqdef function to create one
这很自然,因为sequence_objects
不是序列对象,而是序列对象列表。
如何将seqsubm
函数应用于序列对象列表?
答案 0 :(得分:2)
我不熟悉TraMineR
包,但看起来你试图迭代sequence_objects
的元素。
mapply
用于同时迭代多个对象
相反,lapply
用于迭代单个对象。
因此,以下内容可能对您有用:
costs <- lapply(sequence_objects, seqsubm, method="TRATE")