我认为这是一个初学者问题,但我似乎没有正确的词汇来进行有效的 Google 搜索。
我有一个 data.frame, final
,其中包含一个 列表clusters
,每个列表都是字符串列表。
我想遍历每个集群中的字符串列表:for 循环中的 for 循环。
for (j in final$clusters){
for (i in final$clusters$`j`){
print final$clusters$`j`[i]
}
}
j
对应于中的列表clusters
,并且i
对应于中的项目clusters[j]
我试图通过使用每个集群的长度来做到这一点,我认为这类似于length(final$clusters[1])
,但这给出了 1,而不是列表的长度。
此外,final$clusters[1]
给出 $'1',并在下一行给出簇 1 中的所有字符串。
谢谢。
编辑:dput(str(final))
按要求输出:
List of 2
$ clusters :List of 1629
..$ 1 :
..$ 2 :
..$ 3 :
..$ 4 :
..$ 5 :
..$ 6 :
..$ 7 :
..$ 8 :
..$ 9 :
..$ 10 :
.. [list output truncated]
$ cluster_stats: num [1:1629, 1:6] 0.7 0.7 0.7 0.7 0.7 0.7 ...
..- attr(*, "dimnames")=List of 2
.. ..$ : chr [1:1629] "1" "2" "3" "4" ...
.. ..$ : chr [1:6] "min" "qu1" "median" "mean" ...
NULL