我创建了许多函数,我想将它们应用于列表的子列表(即嵌套列表)。
例如
# Getintervalduration
list.dur <- function(x){
# x is a (non nested...) list
dur <- x$xmax-x$xmin
return(dur)
}
所以我可以将此功能应用于:
(1) 我列表中的一个简单数据框
list.dur(intrad_02.textgrid$phones)
(2) 我列表中的所有数据框
lapply(intrad_02.textgrid, list.dur)
(3) 我的主列表中的所有嵌套列表和列表中的数据框
durations <- lapply(txtgridnames,list.dur)
第三个例子是我想自动成功地做的事情。
现在,txtgridnames
或多或少对应于names(list)
但我不得不半自动修改它,因为:
R确实允许:
durations <- lapply(c(firstnestedlist, second.nested.list, etc.),list.dur)
但不幸的是不允许这样做:
durations <- lapply(names(list.with.nested.lists.inside),list.dur)
names(list)
当可能更改或希望在不同的研究案例中复制时,这是一个问题。
目前,我的解决方案(在这里思考)是编写一个没有“引号”的名称和逗号作为 eol 的表,读取我刚刚使用名称创建的这个文件,然后手动创建一个具有所有名称的对象.
write.table(x=names(corpus),file="txtgridnames.txt", quote=FALSE, row.names=FALSE, append=FALSE, eol=",",col.names=FALSE)
txtgridnames <- readLines("txtgridnames.txt")
txtgridnames
!手动复制输出!..并手动粘贴,这样:
txtgridnames<-c(cnf_01_be,cnf_02_ch[...])
如何自动让R理解list(names(mainlist)),即如何自动创建这个对象“txtgridnames”?
这是我正在处理的数据结构的一个示例:(构成我的语料库的主列表中的 24 个子列表中的大约 502 个数据帧)
以前的帖子还更准确地描述了我正在处理的数据类型(要了解 xmin 和 xmax 对应的内容,请单击此处
#-------------------------------------------
## Data structure example
$ cnf_01_be :List of 17
..$ phones :'data.frame': 2505 obs. of 3 variables:
.. ..$ xmin : num [1:2505] 0 0.427 0.472 0.512 0.562 ...
.. ..$ xmax : num [1:2505] 0.427 0.472 0.512 0.562 0.632 ...
.. ..$ value: chr [1:2505] "_" "v" "w" "a" ...
..$ syll :'data.frame': 1146 obs. of 3 variables:
.. ..$ xmin : num [1:1146] 0 0.427 0.562 0.752 0.952 ...
.. ..$ xmax : num [1:1146] 0.427 0.562 0.752 0.952 1.022 ...
.. ..$ value: chr [1:1146] "_" "vwa" "la" "do~k" ...
..$ delivery :'data.frame': 1146 obs. of 3 variables:
.. ..$ xmin : num [1:1146] 0 0.427 0.562 0.752 0.952 ...
.. ..$ xmax : num [1:1146] 0.427 0.562 0.752 0.952 1.022 ...
.. ..$ value: chr [1:1146] "_" "" "" "" ...
..$ link :'data.frame': 806 obs. of 3 variables:
.. ..$ xmin : num [1:806] 0 0.427 0.752 0.952 1.082 ...
.. ..$ xmax : num [1:806] 0.427 0.752 0.952 1.082 1.256 ...
.. ..$ value: chr [1:806] "_" "" "" "l" ...
..$ words :'data.frame': 806 obs. of 3 variables:
.. ..$ xmin : num [1:806] 0 0.427 0.752 0.952 1.082 ...
.. ..$ xmax : num [1:806] 0.427 0.752 0.952 1.082 1.256 ...
.. ..$ value: chr [1:806] "_" "voilà" "donc" "vous" ...
..$ POS :'data.frame': 806 obs. of 3 variables:
.. ..$ xmin : num [1:806] 0 0.427 0.752 0.952 1.082 ...
.. ..$ xmax : num [1:806] 0.427 0.752 0.952 1.082 1.256 ...
.. ..$ value: chr [1:806] "_" "VER:pres:entatif" "CON:coo" "PRO:sjt" ...
..$ discourse :'data.frame': 750 obs. of 3 variables:
.. ..$ xmin : num [1:750] 0 0.427 0.752 0.952 1.082 ...
.. ..$ xmax : num [1:750] 0.427 0.752 0.952 1.082 1.256 ...
.. ..$ value: chr [1:750] "SIL:l" "MD" "CON" "" ...
..$ disfluency :'data.frame': 806 obs. of 3 variables:
.. ..$ xmin : num [1:806] 0 0.427 0.752 0.952 1.082 ...
.. ..$ xmax : num [1:806] 0.427 0.752 0.952 1.082 1.256 ...
.. ..$ value: chr [1:806] "SIL:l" "" "" "" ...
..$ boundary-dismo:'data.frame': 750 obs. of 3 variables:
.. ..$ xmin : num [1:750] 0 0.427 0.752 0.952 1.082 ...
.. ..$ xmax : num [1:750] 0.427 0.752 0.952 1.082 1.256 ...
.. ..$ value: chr [1:750] "MAJ" "MAJ" "MINg" "" ...
..$ speaker :'data.frame': 1 obs. of 3 variables:
.. ..$ xmin : num 0
.. ..$ xmax : num 244
.. ..$ value: chr "1"
..$ ortho :'data.frame': 101 obs. of 3 variables:
.. ..$ xmin : num [1:101] 0 0.427 3.571 4.3 7.659 ...
.. ..$ xmax : num [1:101] 0.427 3.571 4.3 7.659 8.341 ...
.. ..$ value: chr [1:101] "_" "voilà donc vous avez bien perçu même intuitivement qu'y avait euh" "_" "des choses issues de radios commerciales ou de radios classiques etcaetera" ...
..$ rection :'data.frame': 117 obs. of 3 variables:
.. ..$ xmin : num [1:117] 0 0.427 0.752 0.952 7.659 ...
.. ..$ xmax : num [1:117] 0.427 0.752 0.952 7.659 8.341 ...
.. ..$ value: chr [1:117] "" "md" "md" "urv" ...
..$ sequences :'data.frame': 183 obs. of 3 variables:
.. ..$ xmin : num [1:183] 0 0.427 0.752 0.952 1.942 ...
.. ..$ xmax : num [1:183] 0.427 0.752 0.952 1.942 2.848 ...
.. ..$ value: chr [1:183] "" "md" "md" "SV" ...
..$ boundary :'data.frame': 0 obs. of 3 variables:
.. ..$ xmin : num(0)
.. ..$ xmax : num(0)
.. ..$ value: chr(0)
..$ contours :'data.frame': 1143 obs. of 3 variables:
.. ..$ xmin : num [1:1143] 0 0.427 0.562 0.752 0.952 ...
.. ..$ xmax : num [1:1143] 0.427 0.562 0.752 0.952 1.022 ...
.. ..$ value: chr [1:1143] "" "" "C" "" ...
..$ bdu-auto :'data.frame': 53 obs. of 3 variables:
.. ..$ xmin : num [1:53] 0 0.427 7.659 8.341 18.861 ...
.. ..$ xmax : num [1:53] 0.427 7.659 8.341 18.861 19.624 ...
.. ..$ value: chr [1:53] "" "bdu-1-c_C" "" "bdu-2-s_C" ...
..$ EPG :'data.frame': 54 obs. of 3 variables:
.. ..$ xmin : num [1:54] 0 0.427 0.952 8.341 8.806 ...
.. ..$ xmax : num [1:54] 0.427 0.952 8.341 8.806 19.624 ...
.. ..$ value: chr [1:54] "" "PGC-R" "" "PGC-R" ...
$ cnf_02_ch :List of 17
..$ phones :'data.frame': 2304 obs. of 3 variables:
.. ..$ xmin : num [1:2304] 0 0.001 0.219 0.866 0.886 ...
.. ..$ xmax : num [1:2304] 0.001 0.219 0.866 0.886 0.944 ...
.. ..$ value: chr [1:2304] "_" "n" "y" "m" ...
..$ syll :'data.frame': 1080 obs. of 3 variables:
.. ..$ xmin : num [1:1080] 0 0.001 0.866 1 1.089 ...
.. ..$ xmax : num [1:1080] 0.001 0.866 1 1.089 1.239 ...
.. ..$ value: chr [1:1080] "_" "na~" "m9~t" "" ...
..$ delivery :'data.frame': 1080 obs. of 3 variables:
.. ..$ xmin : num [1:1080] 0 0.001 0.866 1 1.089 ...
.. ..$ xmax : num [1:1080] 0.001 0.866 1 1.089 1.239 ...
.. ..$ value: chr [1:1080] "_" "" "" "" ...
..$ words :'data.frame': 784 obs. of 3 variables:
.. ..$ xmin : num [1:784] 0 0.219 0.866 1.089 1.239 ...
.. ..$ xmax : num [1:784] 0.219 0.866 1.089 1.239 1.691 ...
.. ..$ value: chr [1:784] "_" "humains" "maintenant" "la" ...
..$ link :'data.frame': 784 obs. of 3 variables:
.. ..$ xmin : num [1:784] 0 0.219 0.866 1.089 1.239 ...
.. ..$ xmax : num [1:784] 0.219 0.866 1.089 1.239 1.691 ...
.. ..$ value: chr [1:784] "_" "" "" "" ...
..$ POS :'data.frame': 784 obs. of 3 variables:
.. ..$ xmin : num [1:784] 0 0.219 0.866 1.089 1.239 ...
.. ..$ xmax : num [1:784] 0.219 0.866 1.089 1.239 1.691 ...
.. ..$ value: chr [1:784] "_" "ADJ:adj" "ADV:adv" "DET:def" ...
..$ disfluency :'data.frame': 785 obs. of 3 variables:
.. ..$ xmin : num [1:785] 0 0.219 0.866 1.089 1.239 ...
.. ..$ xmax : num [1:785] 0.219 0.866 1.089 1.239 1.691 ...
.. ..$ value: chr [1:785] "SIL:l" "" "" "CORR-B" ...
..$ discourse :'data.frame': 728 obs. of 3 variables:
.. ..$ xmin : num [1:728] 0 0.219 0.866 1.089 1.239 ...
.. ..$ xmax : num [1:728] 0.219 0.866 1.089 1.239 1.691 ...
.. ..$ value: chr [1:728] "SIL:l" "" "" "" ...
..$ boundary-dismo:'data.frame': 728 obs. of 3 variables:
[...]
提前致谢 !